如何在EF / EF Core的第二张表上以某种条件实现左联接?

时间:2019-02-17 08:05:30

标签: c# sql-server linq-to-sql entity-framework-core

当B具有Where子句时,如何从表A(整行)中选择数据并与表B连接?

我真正需要的是这样的SQL代码:

select * from HISBaseInsurs i left join (select * from HISBaseCenterCodeSends h where h.ServiceGroupID = 4 and h.CenterCode = 2) s on i.ID = s.InsurID

结果:

ID          Name                                               ID          CenterCode  ServiceGroupID InsurID     CodeSend        WebServiceAddress                                                                                    WebServicePassword                                 WebServiceUserName
----------- -------------------------------------------------- ----------- ----------- -------------- ----------- --------------- ---------------------------------------------------------------------------------------------------- -------------------------------------------------- --------------------------------------------------
1           a                                                  2           2           4              1           asd6541         www.x.com                                                                                            23d                                                asda
2           b                                                  NULL        NULL        NULL           NULL        NULL            NULL                                                                                                 NULL                                               NULL
3           c                                                  NULL        NULL        NULL           NULL        NULL            NULL                                                                                                 NULL                                               NULL
4           d                                                  NULL        NULL        NULL           NULL        NULL            NULL                                                                                                 NULL                                               NULL

现在,我希望这些实体像实体列表一样。我所做的是:

list = HISBaseInsurs.Include(s => s.CenterCodeSends.Where(x => x.Center.CenterCode == 2 && x.ServiceGroup.ID == 4)).ToList();

但是此解决方案有一个例外。异常消息是:

  

Include属性lambda表达式's => {来自   HISBaseCenterCodeSend x in s.CenterCodeSends其中   (([[x] .Center.CenterCode == 2)AndAlso([x] .ServiceGroup.ID == 4))   选择[x]}'无效。表达式应表示一个属性   访问:“ t => t.MyProperty”。定位在派生时声明的导航   类型,请指定目标的显式输入的lambda参数   类型,例如'(派生d)=> d.MyProperty'。有关更多信息   包括相关数据,请参阅   http://go.microsoft.com/fwlink/?LinkID=746393

我该如何解决?

1 个答案:

答案 0 :(得分:0)

类似的东西:

var filteredCenterCodeSends = dbContext.HISBaseCenterCodeSends
    .Include( ccs => ccs.Insur ) // assuming navigation property name
    .Where( ccs => 
        ccs.Center.CenterCode == 2 
        && ccs.ServiceGroup.ID == 4 );
        // if ccs.Insur/ID is nullable, also add `&& ccs.Insur != null`

var insurersWithFilteredCcs = dbContext.HISBaseInsurs
    .GroupJoin( filteredCenterCodeSends,
        insr => insr,
        ccs => ccs.Insur,
        (insr, ccsCollection) =>
            new 
            {
                Insur = insr,
                FilteredCenterCodeSends = ccsCollection,
            } );