本文介绍了由于 <AnonymousType>,无法将 .Union 与 Linq 一起使用.的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

限时删除!!

我有点被这个问题困住了.希望我能得到一些帮助.这就是重点.我必须用那个 SQL 请求填充我的 DataGridView :

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

首先,我在第二个SELECT"中使用了null"值,因为UNION"需要像第一个SELECT"这样的 3 个参数,并且我的表 LOT 中没有NomEmploye"数据.无论如何,该请求在 SQL 中运行良好.但是当我尝试在 LINQ 中使用它时

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

我在 Visual Studio 中有那 3 个错误,但我真的不明白.

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

我可能需要一个使用,但哪个?(我确切地说我已经使用 using System.Linq;)

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

但我仍然有这些错误

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.

一个是 { ?数,?EtatLot,字符串 NomEmploye } 另一个是 { ?数,?EtatLot,字符串测试}.最后一个成员具有不同的名称,因此它是不同的类型.

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.

试试这个:

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>,无法将 .Union 与 Linq 一起使用.的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

1403页,肝出来的..

09-06 21:33