2015-03-04 12 views
9

Bu sorun ile sıkışmış durumdayım. Umarım biraz yardım edeceğim. İşte nokta. O SQL isteği ile benim DataGridView doldurmak zorunda yer:Kullanılamıyor Linq ile Linq nedeniyle <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é' 

İlk ben kullandım "boş" değeri benim ikinci "BİRLİĞİ" gibi 3 argümanları olması gerekir, çünkü "SEÇ" ilk "SEÇ" ve Tablo LOT'umda "NomEmploye" verisi yok. Neyse, bu istek SQL'de iyi çalışıyor. Ama 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(); 

kullanmak çalıştığımda 3 Visual Studio hataları ve ben gerçekten yok bunu anlamak o var.

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. 

i görebileceğiniz gibi

, sorunun <AnonymousType> kaynaklanmaktadır .... Ama o kadar çok şu an bana yardım etmez. Ben de o

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

çalışmak Ama aynı hataları var olmak için bir yol artı bu bir

belki bir kullanma ihtiyacım

ama hangisi denedim? (Zaten ben)

Son olarak, bu son yöntemi denedim.

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

Ama hala o büyük blok için bu hatalar

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

Üzgünüz var ama seni istemeden önce ben ne yaptığını tüm açıklamaya çalıştık. Gelecekte vereceğiniz cevaplar için teşekkürler. Sadece son denemede baktığımızda

+1

İyi ilk kez soru! – Enigmativity

cevap

5

Sorun, anonim türlerin aynı türde olmamasıdır.

Biri { ? NumLot, ? EtatLot, string NomEmploye } ve diğeri { ? NumLot, ? EtatLot, string test }. Son üyenin farklı bir ismi var, dolayısıyla farklı bir tip.

yerine bu deneyin:

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

LOT teşekkürler! Şimdi biliyorum ki aynı adaya ihtiyacım var :) Bu yüzden ben bu şekilde 'yeni seçtim {x.NumLot, x.EtatLot, NomEmploye = "Olumsuz" " – Siick

+1

@Siick - Topluluğa yeni başlayan bir kullanıcı olarak, http://meta.stackexchange.com/questions/5234/how-does-accepting-an-answer-work adresini ziyaret edebilirsiniz. Bu yardımcı olsaydı cevabı kabul et :) –

2

Bunu görmek:

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

vs

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

atayabilir veya tüm sürece başka anonim tip bir kadro listesinde asla onların özellikleri eşit türü ve adı yok.

DÜZENLEME: Yazmak yerine select new { x.NumLot, x.EtatLot, NomEmploye = test }; yazın.

+1

Sorunu açıklayan, ancak bir çözüm sağlamayan. – jessehouwing

+0

@jessehouwing bu doğru, ben bir çözüm güncellendi – HimBromBeere