首页 > 解决方案 > 使用多个带有 into 的 LINQ 语句,因为左外连接的 DefaultIfEmpty() 不起作用

问题描述

我正在尝试使用带有 DefaultIfEmpty() 的“into”语句进行左外连接,但是当我尝试加入第三个集合时,它不喜欢它(看不到/使用它/找到它)

它似乎不喜欢personRole下面的行

join roleTypes in roles on personRole.ContactRoleTypeId equals roleTypes.ContactRoleTypeId into r2

查询:

findPersonResultsViewModelNew =
         from azed in findPersonViewModel.findPersonResultsViewModel
         join personRole in personContactRoles on azed.PersonID equals personRole.PersonId into r1
         join roleTypes in roles on personRole.ContactRoleTypeId equals roleTypes.ContactRoleTypeId into r2
         from p in r1.DefaultIfEmpty()
         from g in r2.DefaultIfEmpty()
         select
         //…. other code 

标签: c#.netlinqcollectionslinq-to-sql

解决方案


改变你的陈述的顺序。进行左连接时,您必须立即join使用新的from... DefaultIfEmpty()

findPersonResultsViewModelNew =
         from azed in findPersonViewModel.findPersonResultsViewModel
         join personRole in personContactRoles on azed.PersonID equals personRole.PersonId into prj
         from personRole in prj.DefaultIfEmpty()
         join roleTypes in roles on personRole.ContactRoleTypeId equals roleTypes.ContactRoleTypeId into rtj
         from roleTypes in rtj.DefaultIfEmpty()
         select

推荐阅读