由于<anonymous type> </anonymous type>,可以与Linq一起使用.Union

时间:2015-03-04 12:03:55

标签: c# sql-server linq datagridview

我有点不解决这个问题。希望我会得到一些帮助。 这就是重点。 我必须使用该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é' or EtatLot='Suspendu' or EtatLot='Démarré'
UNION
SELECT NumLot, EtatLot, null FROM LOT
WHERE EtatLot='Démarré'

首先我在第二个“SELECT”中使用了“null”值,因为“UNION”需要有3个参数,如第一个“SELECT”,并且我的表LOT中没有“NomEmploye”数据。 无论如何,该请求在SQL中运行良好。 但是当我尝试在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();

我在Visual Studio中有3个错误,我真的不明白它。

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.

正如我所看到的,问题是由于<AnonymousType> ......但是现在这对我没那么帮助。 我也试过这种方式让它发挥作用

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

但我有同样的错误加上那个

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

我可能需要使用,但哪一个? (我确切地说我已经使用using System.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 };
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();

但我仍然有这些错误

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

对不起那个大块但是我试着在问你之前解释我所做的一切。 感谢您将来的答案。

2 个答案:

答案 0 :(得分:8)

问题是您的匿名类型不是同一类型。

一个是{ ? 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 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 :(得分:2)

当我只看你的最后一次尝试时,我看到了:

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

VS。

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

只要所有属性的类型和名称不相同,您就永远不能将匿名类型的一个列表分配或强制转换为另一个列表。

编辑:改为为第二个写select new { x.NumLot, x.EtatLot, NomEmploye = test };