asp.net-mvc – Linq离开外连接不起作用
发布时间:2020-12-16 09:52:58 所属栏目:asp.Net 来源:网络整理
导读:使用MSDN文章 “How to: Perform Left Outer Joins (C# Programming Guide)”中的技术,我尝试在我的Linq代码中创建一个左外连接.本文提到使用DefaultIfEmpty方法从组连接创建左外连接.基本上,它指示程序包括左(第一)集合的结果,即使在正确的集合中没有结果.
使用MSDN文章
“How to: Perform Left Outer Joins (C# Programming Guide)”中的技术,我尝试在我的Linq代码中创建一个左外连接.本文提到使用DefaultIfEmpty方法从组连接创建左外连接.基本上,它指示程序包括左(第一)集合的结果,即使在正确的集合中没有结果.
但是,这个程序的执行方式就像没有指定外部联接一样. 在我们的数据库中,AgentProductTraining是我们的代理人所采用的课程集合.通常,如果没有在CourseMaterials表中输入相应的值,则无法在其适当的表格中输入课程.但是,偶尔会发生这种情况,所以我们要确保即使在CourseProductTraining中列出课程而没有CourseMaterials中的任何相应信息,我们也会返回结果. var training = from a in db.AgentProductTraining join m in db.CourseMaterials on a.CourseCode equals m.CourseCode into apm where a.SymNumber == id from m in apm.DefaultIfEmpty() where m.EffectiveDate <= a.DateTaken && ((m.TerminationDate > a.DateTaken) | (m.TerminationDate == null)) select new { a.AgentProdTrainId,a.CourseCode,a.Course.CourseDescription,a.Course.Partner,a.DateTaken,a.DateExpired,a.LastChangeOperator,a.LastChangeDate,a.ProductCode,a.Product.ProductDescription,m.MaterialId,m.Description,a.Method }; 解决方法
MSDN示例使用新的变量子界面:
var query = from person in people join pet in pets on person equals pet.Owner into gj from subpet in gj.DefaultIfEmpty() select new { person.FirstName,PetName = (subpet == null ? String.Empty : subpet.Name) }; 所以你必须使用自己的“subpet”,我使用submat变量重写了你的代码: var training = from a in db.AgentProductTraining join m in db.CourseMaterials on a.CourseCode equals m.CourseCode into apm where a.SymNumber == id from submat in apm.DefaultIfEmpty() where (submat.EffectiveDate <= a.DateTaken || submat.EffectiveDate == null) && (submat.TerminationDate > a.DateTaken || submat.TerminationDate == null) select new { a.AgentProdTrainId,MaterialId = (submat==null?-1:submat.MaterialId),Description = (submat==null?String.Empty:submat.Description),a.Method }; (编辑:李大同) 【声明】本站内容均来自网络,其相关言论仅代表作者个人观点,不代表本站立场。若无意侵犯到您的权利,请及时与联系站长删除相关内容! |
推荐文章
站长推荐
- 详解Asp.net web.config customErrors 如何设置
- asp.net-mvc – 为什么我的ActionFilters都没有运
- asp.net-web-api – 为ASP.NET Web API应用程序生
- asp.net-core – 具有取消令牌的自定义AspCore中
- asp.net-mvc – 使用ASP.NET MVC,如何在外部控制
- ASP.Net应用程序是否有内存限制?
- asp.net-mvc – 存储库层中的服务层重复功能
- asp页面上的失败会话到asp.net页面
- asp.net – 如何在网格行中显示枚举描述或名称?
- asp.net-mvc – 如何在ASP.NET MVC中添加路由到动
热点阅读