热门标签 | HotTags
当前位置:  开发笔记 > 编程语言 > 正文

C#学习教程:由于,无法与Linq一起使用.Union分享

由于,无法与Linq一起使用.Union我有点困惑于这个问题。希望我会得到一些帮助。这就是重点。我必须用我的SQL请求填充我的DataGridView:SELECTLOT.NumL

由于,无法与Linq一起使用.Union

我有点困惑于这个问题。 希望我会得到一些帮助。 这就是重点。 我必须用我的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 ' Error 2 'System.Linq.IQueryable ' does not contain a definition for 'Union' and the best overload the extension method 'System.Linq.ParallelEnumerable.Union  (System.Linq.ParallelQuery , System.Collections.Generic.IEnumerable ) ' Error 3 type arguments for method 'System.Linq.Enumerable.ToList  (System.Collections.Generic.IEnumerable )' can not be inferred from the use. Try specifying the type arguments explicitly. 

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

 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 ' does not contain a definition for 'Union' and the best overload the extension method 'System.Linq.ParallelEnumerable.Union  (System.Linq.ParallelQuery , System.Collections.Generic.IEnumerable ) 'contains invalid arguments Error 2 Argument instance: can not convert 'System.Linq.IQueryable ' to 'System.Linq.ParallelQuery ' 

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

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

一个是{ ? NumLot, ? EtatLot, string NomEmploye } { ? NumLot, ? EtatLot, string NomEmploye } { ? NumLot, ? EtatLot, string NomEmploye }和另一个是{ ? NumLot, ? EtatLot, string test } { ? NumLot, ? EtatLot, string test } { ? 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, emp.NomEmploye }; 

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

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

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

上述就是C#学习教程:由于,无法与Linq一起使用.Union分享的全部内容,如果对大家有所用处且需要了解更多关于C#学习教程,希望大家多多关注—编程笔记


推荐阅读
author-avatar
mobiledu2502903757
这个家伙很懒,什么也没留下!
PHP1.CN | 中国最专业的PHP中文社区 | DevBox开发工具箱 | json解析格式化 |PHP资讯 | PHP教程 | 数据库技术 | 服务器技术 | 前端开发技术 | PHP框架 | 开发工具 | 在线工具
Copyright © 1998 - 2020 PHP1.CN. All Rights Reserved | 京公网安备 11010802041100号 | 京ICP备19059560号-4 | PHP1.CN 第一PHP社区 版权所有