无法使用。由于< AnonymousType> [英] Can't use .Union with Linq due to <AnonymousType>
问题描述
这是要点。
我必须使用该SQL请求填写我的DataGridView:
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é'或EtatLot ='Suspendu'或EtatLot ='Démarré'
UNION
SELECT NumLot,EtatLot,null FROM LOT
WHERE EtatLot ='Démarré'
首先我在第二个SELECT中使用了null值,因为UNION需要有第一个SELECT的3个参数,表中没有NomEmploye数据。
无论如何,请求在SQL中运行良好。
但是当我尝试在LINQ中使用它时
string test =null;
var listeLotControle =(from x in entBoum.LOT
join in in entBoum.AFFECTATION_LOT on x.NumLot equals aff.NumLot
join emp in entBoum.EMPLOYE on aff.IdEmploye equals emp.IdEmploye
其中x.EtatLot.Contains(Libéré)|| x.EtatLot.Contains(Suspendu)|| x.EtatLot.Contains(Démarré)
选择新的{x.NumLot,联合
(来自x in entBoum.LOT
其中x.EtatLot.Contains(Démarré)
选择新的{x.NumLot,x。 EtatLot,test});
dataGridViewAffectationLotControleur.DataSource = listeLotControle.ToList();
我在Visual Studio中有3个错误,我不太明白。
错误1参数实例:无法将System.Linq.IQueryable AnonymousType#1>转换为System.Linq.ParallelQuery< AnonymousType# 2>'
错误2'System.Linq.IQueryable< AnonymousType#1>'不包含'Union'的定义,并且最佳重载扩展方法'System.Linq.ParallelEnumerable.Union< TSource> (System.Linq.ParallelQuery< TSource> System.Collections.Generic.IEnumerable< TSource>)'
错误3方法'System.Linq.Enumerable.ToList< TSource>的类型参数(System.Collections.Generic.IEnumerable< TSource>)'不能从使用中推断出来。尝试显式指定类型参数。
如我所见,问题是由于< AnonymousType>
....但是现在不能帮助我。
我也尝试过这样的方式使其工作。
IQueryable listeLotControle;
string test =null;
listeLotControle =(从x在entBoum.LOT
中加入aff中的entBoum.AFFECTATION_LOT x.NumLot等于aff.NumLot
在entBoum.EMPLOYE中加入emp对于aff.IdEmploye等于emp.IdEmploye
其中x.EtatLot.Contains(Libéré)|| x.EtatLot.Contains(Suspendu)|| x.EtatLot.Contains(Démarré)
选择新的{x.NumLot,x .EtatLot,emp.NomEmploye})。Union
(from x in entBoum.LOT
其中x.EtatLot.Contains(Démarré)
选择新的{x.NumLot,x.EtatLot ,test});
dataGridViewAffectationLotControleur.DataSource = listeLotControle.ToList();
但是我有相同的错误加上一个
错误4'System.Linq.IQueryable'不包含'ToList'的定义,并且没有扩展方法'ToList'接受类型为'System.Linq.IQueryable的第一个参数'被发现(使用指令或程序集参考是否缺少?)
我可能需要使用,但哪一个? (我确切地说,我已经使用使用System.Linq;
)
最后我尝试了最后一个方法。
string test =null;
var listeLotControle = from x in entBoum.LOT
在x.NumLot上的entBoum.AFFECTATION_LOT中加入aff等于aff.NumLot
在entBoum.EMPLOYE中加入emp对于aff.IdEmploye等于emp.IdEmploye
其中x.EtatLot.Contains(Libéré)|| x.EtatLot.Contains(Suspendu)|| x.EtatLot.Contains(Démarré)
选择新的{x.NumLot,x.EtatLot,emp.NomEmploye};
var listeLotControle2 = from in in EntBoum.LOT
其中x.EtatLot.Contains(Démarré)
选择新的{x.NumLot,x.EtatLot,test};
var union = listeLotControle.Union(listeLotControle2);
dataGridViewAffectationLotControleur.DataSource = listeLotControle2.ToList();
但我仍然有这些错误
错误1'System.Linq.IQueryable< AnonymousType#1>'不包含'Union'的定义,并且最佳重载扩展方法'System.Linq.ParallelEnumerable.Union< TSource> (System.Linq.ParallelQuery< TSource>,System.Collections.Generic.IEnumerable< TSource>)'包含无效的参数
错误2参数实例:无法转换'System.Linq.IQueryable< AnonymousType#1> ;'to'System.Linq.ParallelQuery< AnonymousType#2>'
对于那个大块但是我已经试图解释我在做什么之前所做的一切。
感谢你将来的答案。
问题是你的匿名类型是不一样的。 p>
一个是 {? NumLot,? EtatLot,string NomEmploye}
,另一个是 {? NumLot,? EtatLot,string test}
。最后一位成员名称不同,因此是不同的类型。
请尝试这样做:
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等于emp.IdEmploye
其中x.EtatLot.Contains(Libéré)|| x.EtatLot.Contains(Suspendu)|| x.EtatLot.Contains(Démarré)
选择新的{x.NumLot,x.EtatLot,emp.NomEmploye}
).Union
(
from x in entBoum.LOT
其中x.EtatLot。包含(Démarré)
选择新的{x.NumLot,x.EtatLot,NomEmploye = test}
);
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.
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 }
);
这篇关于无法使用。由于< AnonymousType>的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!