使用Entity Framework Core进行LEFT JOIN查询

时间:2018-06-12 15:47:56

标签: c# entity-framework-core

我做了很多研究并阅读了一些类似的问题和答案,但仍然不知道如何编写EF Core代码来创建以下SQL服务器查询。有人可以帮忙吗我已经看过教程解释如何使用LEFT JOIN连接2个表,但是找不到包含3个或更多表的任何表。

    var results=await (from sso in context.StandardServices
                       join mos in context.Sorted on sso.ServiceCode equals mos.ServiceCode
                       join mad in context.ServiceDetails
                       on new { key1 = sso.DocID, key2 = parentId } equals new { key1 = (Guid)mad.ServiceTypeUnid, key2 = mad.ParentRecordUnid }
                                                      into jointable
                         where sso.IsDeleted == "N" && sso.TopService =="Y" 
                         orderby mos.SortOrder
                         from mad1 in jointable.DefaultIfEmpty()
                         select new ServiceRowDetails()
                         {
                             DocID = mad1.DocID,
                             ParentRecordUnid = mad1.ParentRecordUnid,
                             ServiceTypeUnid = sso.DocID,
                             ServiceType = sso.Description,
                             ParticipantCount = mad1.ParticipantCount ?? 0,
                             StandardFees = sso.StandardRate ?? 0,
                             StandardHours = sso.StandardHours ?? 0,
                             TotalFees = mad1.TotalFees ?? 0,
                             TotalHours = mad1.TotalHours ?? 0
                         }).ToListAsync();

以下是接近上面的内容(我已经替换了一些文本以保护隐私,因此可能不完全匹配)。但由于某些原因,它会选择所有列而不是提到的列。此外,它最后添加了一个(SELECT 1)。

    var results=await (from sso in context.StandardServices
                           join mos in context.Sorted on sso.ServiceCode equals mos.ServiceCode
                           join mad in context.ServiceDetails
                           on new { key1 = sso.DocID, key2 = parentId } equals new { key1 = (Guid)mad.ServiceTypeUnid, key2 = mad.ParentRecordUnid }
                                                          into jointable
 from mad1 in jointable.DefaultIfEmpty()
                             where sso.IsDeleted == "N" && sso.TopService =="Y" 
                             orderby mos.SortOrder

                             select new ServiceRowDetails()
                             {
                                 DocID = mad1.DocID,
                                 ParentRecordUnid = mad1.ParentRecordUnid,
                                 ServiceTypeUnid = sso.DocID,
                                 ServiceType = sso.Description,
                                 ParticipantCount = mad1.ParticipantCount ?? 0,
                                 StandardFees = sso.StandardRate ?? 0,
                                 StandardHours = sso.StandardHours ?? 0,
                                 TotalFees = mad1.TotalFees ?? 0,
                                 TotalHours = mad1.TotalHours ?? 0
                             }).ToListAsync();

更新:@IvanStoev在下面发表了评论,并且工作正常。

{{1}}

0 个答案:

没有答案