对in使用多个LINQ语句,以使左外部联接的DefaultIfEmpty()不起作用

时间:2019-06-26 07:46:43

标签: c# .net linq collections linq-to-sql

我试图将DefaultInEmpty()的“ 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 

1 个答案:

答案 0 :(得分:1)

更改语句的顺序。进行左联接时,必须立即在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
相关问题