EF Core 中左连接

问题描述 投票:0回答:1
 var query = (from pn in PriorNavPositions 
         join sa in SourceDataset.SourceInvestorAllocations  on
             new { SourceDatasetId = pn.SourceDatasetId, Iqid = pn.SubscriberFiid, ShareIndex = pn.ShareIndex } equals
             new { SourceDatasetId = sa.SourceDataset.Id, Iqid = sa.IqId, ShareIndex = sa.EFrontInteger } 
         join ea in EfrontNavDataset.EfrontNavInvestorAllocations on
             new { SourceDatasetId = sa.SourceDataset.Id, Iqid = sa.IqId, ShareIndex = sa.EFrontInteger } equals
             new { SourceDatasetId = ea.EfrontNavDataset.SourceDatasetId, Iqid = ea.IqId, ShareIndex = ea.ShareIndex } into pnj
         from ea in pnj.DefaultIfEmpty()
         select new { sa, ea, pn }

我正在此 EF 查询中寻找左连接,以获取表 PriorNavPositions 中的所有记录。

entity-framework left-join
1个回答
0
投票

使用群组加入(例如:

join _ in _ on _ equals _ into _
),然后对群组加入的结果调用
DefaultIfEmpty

from pn in PriorNavPositions
    join sa in SourceDataSet.SourceInvestorAllocations
    on new { ... } equals new { ... }
    into groupJoinResult
    from gjr in groupJoinResult.DefaultIfEmpty()

https://learn.microsoft.com/en-us/dotnet/csharp/linq/standard-query-operators/join-operations#perform-left-outer-joins

© www.soinside.com 2019 - 2024. All rights reserved.