添加Linq表联接限制了我的结果,因为它确实需要是左外部联接

时间:2019-06-26 00:30:47

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

我有一个运行良好的查询,但是我需要加入一个数据库驱动的上下文表,该表包含id的值

我确实有25条记录,但是现在只有3条记录,因为联接仅存在3条记录中 这是我添加的单行

join roleTypes in context.ContactRoleTypes on personRole.ContactRoleTypeId equals roleTypes.ContactRoleTypeId

我确实在选择的这一行ContactType = p == null ? string.Empty : p.ContactRoleName,中添加了显示结果

findPersonResultsViewModelNew = from azed in findPersonViewModel.findPersonResultsViewModel
                        join personRole in personContactRoles on azed.PersonID equals personRole.PersonId
                        join roleTypes in context.ContactRoleTypes on personRole.ContactRoleTypeId equals roleTypes.ContactRoleTypeId
                        into r1
                        from p in r1.DefaultIfEmpty()
                        select
                        new FindPersonResultsViewModel
                        {
                            PersonID = azed.PersonID,
                            AZEDID = azed.AZEDID,
                            FirstName = azed.FirstName,
                            MiddleName = azed.MiddleName,
                            LastName = azed.LastName,
                            ContactRoleTypeId = p == null ? 0 : p.ContactRoleTypeId,                                
                            ContactType = p == null ? string.Empty : p.ContactRoleName,
                            IsInContactManager = p == null ? false : true,
                            ExistInContactManager = p == null ? false : true,
                            ActionType = p == null ? false : true,
                        };

您可以看到有3个源,其中2个在内存中,然后我加入了context.ContactRoleTypes,但这需要是一个左外部联接,在LINQ中,您似乎必须执行类似from w in r2.DefaultIfEmpty()

更新:
所以如果我这样做

join roleTypes in context.ContactRoleTypes on personRole.ContactRoleTypeId equals roleTypes.ContactRoleTypeId into r2
   from p in r1.DefaultIfEmpty()
   from g in r2.DefaultIfEmpty()

我收到错误personRole does not exist in the current context。如果我从均等交换,则双方都有错误。

0 个答案:

没有答案