【发布时间】: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.
正如我所见,问题出在<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,但是哪一个? (我确切地说我已经在使用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