【问题标题】:Can't use .Union with Linq due to <AnonymousType>由于 <AnonymousType>,无法将 .Union 与 Linq 一起使用
【发布时间】:2015-03-04 12:03:55
【问题描述】:

我有点被这个问题困扰。希望我能得到一些帮助。 这就是重点。 我必须用那个 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”需要像第一个“SELECT”一样有 3 个参数,并且我的 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.

正如我所见,问题出在&lt;AnonymousType&gt;.... 但这对我现在没有多大帮助。 我也试过用这种方法让它工作

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,但是哪一个? (我确切地说我已经在使用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>'

对不起,我在问你之前试图解释我所做的一切。 感谢您未来的回答。

【问题讨论】:

  • 第一次问好!

标签: c# sql-server linq datagridview


【解决方案1】:

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

一个是{ ? 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 }
);

【讨论】:

  • 非常感谢!现在我知道我需要同名 :) 所以我已经按照你的方式这样做了select new { x.NumLot, x.EtatLot, NomEmploye = "Non affecté" }
  • @Siick - 作为社区的新手,您可以访问meta.stackexchange.com/questions/5234/…,如果对您有帮助,请接受答案:)
【解决方案2】:

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

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

对比

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

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

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

【讨论】:

  • 这解释了问题,但没有提供解决方案。
  • @jessehouwing 没错,我已经更新了一个解决方案
猜你喜欢
  • 1970-01-01
  • 2016-10-01
  • 1970-01-01
  • 2016-05-06
  • 1970-01-01
  • 2013-08-07
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多