2015-03-04 71 views
9

Sono tipo bloccato con quel problema. Spero di avere un po 'di aiuto. Ecco il punto. devo riempire il mio DataGridView a tale richiesta SQL: valore "null"Impossibile utilizzare .Union con Linq a causa di <AnonymousType>

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

Prima ho usato nel mio secondo "SELECT", perché "UNION" bisogno di avere 3 argomenti come il primo "SELECT" e non ci sono dati "NomEmploye" nella mia tabella LOT. Ad ogni modo quella richiesta funziona bene in SQL. ma quando provo ad usarlo 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(); 

ho che 3 errori in Visual Studio e io in realtà non lo capiscono.

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. 

Come posso vedere, il problema è dovuto al <AnonymousType> .... Ma questo non mi aiuti così tanto in questo momento. Ho anche provato in quel modo per farlo funzionare

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

ma ho gli stessi errori più che uno

Ho forse bisogno di un utilizzo, ma quale? (Preciso che uso già using System.Linq;)

Finalmente ho provato quell'ultimo metodo.

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

ma ho ancora questi errori

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

dispiace per quel grande blocco, ma ho cercato di spiegare tutto quello che ho fatto prima di chiedere. Grazie per le tue future risposte.

+1

Buona domanda per la prima volta! – Enigmativity

risposta

5

Il problema è che i tipi anonimi non sono dello stesso tipo.

Uno è { ? NumLot, ? EtatLot, string NomEmploye } e l'altro è { ? NumLot, ? EtatLot, string test }. L'ultimo membro ha un nome diverso, quindi è un tipo diverso.

Prova a modificare:

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 } 
); 
+1

Grazie mille! Ora so che ho bisogno dello stesso nome :) Quindi ho fatto il tuo modo in questo modo 'selezionare new {x.NumLot, x.EtatLot, NomEmploye = "Non interessato"} ' – Siick

+1

@Siick - Come novizio della comunità, puoi visitare http://meta.stackexchange.com/questions/5234/how-does-accepting-an-answer-work e fare accetta la risposta se questo ti ha aiutato :) –

2

Quando guardo solo al vostro ultimo tentativo vedo questo:

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

vs.

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

Non si può mai assegnare o lanciare un elenco di tipo anonimo ad un altro più a lungo tutta la loro le proprietà non hanno lo stesso tipo e nome.

MODIFICA: Scrivere select new { x.NumLot, x.EtatLot, NomEmploye = test }; per il secondo.

+1

Che spiega il problema, ma non fornisce una soluzione. – jessehouwing

+0

@jessehouwing è vero, ho aggiornato una soluzione – HimBromBeere