No se puede usar .Union con Linq debido a <AnonymousType>
Estoy un poco atrapado con ese problema. Espero obtener ayuda. Aquí está el punto. Tengo que llenar mi DataGridView con esa solicitud SQL:
SELECT LOT.NumLot, EtatLot, NomEmploye FROM LOT
JOIN AFFECTATION_LOT on LOT.NumLot=AFFECTATION_LOT.NumLot
JOIN EMPLOYE on AFFECTATION_LOT.IdEmploye=EMPLOYE.IdEmploye
WHERE EtatLot='Libéré' or EtatLot='Suspendu' or EtatLot='Démarré'
UNION
SELECT NumLot, EtatLot, null FROM LOT
WHERE EtatLot='Démarré'
Primero he usado el valor "nulo" en mi segundo "SELECCIONAR" porque "UNIÓN" necesita tener 3 argumentos como el primer "SELECCIONAR" y no hay datos de "NomEmploye" en mi LOTE de la tabla. De todos modos, esa solicitud está funcionando bien en SQL. Pero cuando trato de usarlo en LINQ
string test = "null";
var listeLotControle = (from x in entBoum.LOT
join aff in entBoum.AFFECTATION_LOT on x.NumLot equals aff.NumLot
join emp in entBoum.EMPLOYE on aff.IdEmploye equals emp.IdEmploye
where x.EtatLot.Contains("Libéré") || x.EtatLot.Contains("Suspendu") || x.EtatLot.Contains("Démarré")
select new { x.NumLot, x.EtatLot, emp.NomEmploye }).Union
(from x in entBoum.LOT
where x.EtatLot.Contains("Démarré")
select new { x.NumLot, x.EtatLot, test });
dataGridViewAffectationLotControleur.DataSource = listeLotControle.ToList();
Tengo esos 3 errores en Visual Studio y realmente no lo entiendo.
Error 1 Argument instance: can not convert 'System.Linq.IQueryable AnonymousType # 1>' to 'System.Linq.ParallelQuery <AnonymousType # 2>'
Error 2 'System.Linq.IQueryable <AnonymousType # 1>' does not contain a definition for 'Union' and the best overload the extension method 'System.Linq.ParallelEnumerable.Union <TSource> (System.Linq.ParallelQuery <TSource>, System.Collections.Generic.IEnumerable <TSource>) '
Error 3 type arguments for method 'System.Linq.Enumerable.ToList <TSource> (System.Collections.Generic.IEnumerable <TSource>)' can not be inferred from the use. Try specifying the type arguments explicitly.
Como puedo ver, el problema se debe a la<AnonymousType>
.... Pero eso no me ayuda mucho en este momento. También he tratado de esa manera para que funcione
IQueryable listeLotControle;
string test = "null";
listeLotControle = (from x in entBoum.LOT
join aff in entBoum.AFFECTATION_LOT on x.NumLot equals aff.NumLot
join emp in entBoum.EMPLOYE on aff.IdEmploye equals emp.IdEmploye
where x.EtatLot.Contains("Libéré") || x.EtatLot.Contains("Suspendu") || x.EtatLot.Contains("Démarré")
select new { x.NumLot, x.EtatLot, emp.NomEmploye }).Union
(from x in entBoum.LOT
where x.EtatLot.Contains("Démarré")
select new { x.NumLot, x.EtatLot, test });
dataGridViewAffectationLotControleur.DataSource = listeLotControle.ToList();
Pero tengo los mismos errores más ese
Error 4 'System.Linq.IQueryable' does not contain a definition for 'ToList' and no extension method 'ToList' accepting a first argument of type 'System.Linq.IQueryable' was found (a using directive or an assembly reference is it missing?)
Tal vez necesito un uso, pero ¿cuál? (Preciso que ya usousing System.Linq;
)
Finalmente probé ese último método.
string test = "null";
var listeLotControle = from x in entBoum.LOT
join aff in entBoum.AFFECTATION_LOT on x.NumLot equals aff.NumLot
join emp in entBoum.EMPLOYE on aff.IdEmploye equals emp.IdEmploye
where x.EtatLot.Contains("Libéré") || x.EtatLot.Contains("Suspendu") || x.EtatLot.Contains("Démarré")
select new { x.NumLot, x.EtatLot, emp.NomEmploye };
var listeLotControle2 = from x in entBoum.LOT
where x.EtatLot.Contains("Démarré")
select new { x.NumLot, x.EtatLot, test };
var union = listeLotControle.Union(listeLotControle2);
dataGridViewAffectationLotControleur.DataSource = listeLotControle2.ToList();
Pero aun tengo estos errores
Error 1 'System.Linq.IQueryable <AnonymousType # 1>' does not contain a definition for 'Union' and the best overload the extension method 'System.Linq.ParallelEnumerable.Union <TSource> (System.Linq.ParallelQuery <TSource>, System.Collections.Generic.IEnumerable <TSource>) 'contains invalid arguments
Error 2 Argument instance: can not convert 'System.Linq.IQueryable <AnonymousType # 1>' to 'System.Linq.ParallelQuery <AnonymousType # 2>'
Perdón por ese gran bloqueo, pero he tratado de explicarte todo lo que hice antes de preguntarte. Gracias por tus futuras respuestas.