如何在EF / EF Core的第二个表上实现具有某些条件的左连接?

问题描述 投票:0回答:1

当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();

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

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

我怎么解决这个问题?

c# sql-server linq-to-sql entity-framework-core
1个回答
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,
            } );
© www.soinside.com 2019 - 2024. All rights reserved.