Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Can't use .Union with Linq due to <AnonymousType>

I'm kind of stuck with that problem. Hope i'll get some help. Here's the point. I have to fill my DataGridView with that SQL request :

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é'

First i've used "null" value in my second "SELECT" because "UNION" need to have 3 arguments like the first "SELECT" and there is no "NomEmploye" data in my table LOT. Anyway that request is working well in SQL. But when i try to use it in 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();

I have that 3 errors in Visual Studio and i don't really understand it.

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.

As i can see, the problem is due to the <AnonymousType>.... But that don't help me so much right now. I've also tried that way to make it work

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();

But i've got same errors plus that one

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?)

I maybe need a Using, but which one? (I precise that i already use using System.Linq;)

Finally i've tried that last method.

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();

But i still have these errors

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>'

Sorry for that big block but i've tried to explain all what i did before asking you. Thanks for your future answers.

like image 978
Siick Avatar asked Mar 04 '15 12:03

Siick


2 Answers

When I only look at your last try I see this:

select new { x.NumLot, x.EtatLot, emp.NomEmploye };

vs.

select new { x.NumLot, x.EtatLot, test };

You can never assign or cast one list of anonymous type to another as long as all their properties do not have equal type and name.

EDIT: Write select new { x.NumLot, x.EtatLot, NomEmploye = test }; for the second one instead.

like image 116
MakePeaceGreatAgain Avatar answered Nov 02 '22 16:11

MakePeaceGreatAgain


The problem is that your anonymous types are not the same type.

One is { ? NumLot, ? EtatLot, string NomEmploye } and the other is { ? NumLot, ? EtatLot, string test }. The last member has a different name hence it is a different type.

Try this instead:

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, NomEmploye = test }
);
like image 10
Enigmativity Avatar answered Nov 02 '22 16:11

Enigmativity