How to implement a left join with some condition on second table in EF / EF Core?

c# entity-framework-core linq-to-sql sql-server

Question

How to select data from table A (whole rows) join with table B when B has a Where clause?

What I need exactly is like this SQL code:

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

Result:

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

Now I want to have these like a list of entities. What I've done is:

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

But this solution has an exception. The exception message is:

The Include property lambda expression 's => {from HISBaseCenterCodeSend x in s.CenterCodeSends where (([x].Center.CenterCode == 2) AndAlso ([x].ServiceGroup.ID == 4)) select [x]}' is invalid. The expression should represent a property access: 't => t.MyProperty'. To target navigations declared on derived types, specify an explicitly typed lambda parameter of the target type, E.g. '(Derived d) => d.MyProperty'. For more information on including related data, see http://go.microsoft.com/fwlink/?LinkID=746393.

How I can fix this?

1
0
2/17/2019 8:05:30 AM

Popular Answer

Something like:

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,
            } );
0
2/20/2019 9:36:48 PM


Related Questions





Related

Licensed under: CC-BY-SA with attribution
Not affiliated with Stack Overflow
Licensed under: CC-BY-SA with attribution
Not affiliated with Stack Overflow