简体   繁体   中英

EFCore Linq Left Outer Join

I've looked through hundreds of examples and still can't quite find what I'm looking for.

public class CA
{
    public int Id {get;set;}
    public List<SignOff> SignOffs {get;set}
}

public class SignOff
{
    public int Id {get;set;}
    public int CAId {get;set;}
    public bool IsCurrentQtr {get;set;}
}

I currently have a search function that returns me an IQueryable<CA> qry . I now need to use that IQueryable<CA> and left join all SignOffs where IsCurrentQtr == true but then I want to select all CA where SignOffs == null || SignOffs.Count == 0 SignOffs == null || SignOffs.Count == 0 .

It needs to use the method syntax. Also, please keep in mind that there are actually around 50 columns of data in the CA table with 15 other FK relationships and the signoff table has around 70 columns of data and it's using SQL Server 2017.

Can someone help me write this linq query?

EDIT 1: FYI, this is puzzling to me too but this is the way it was explained to me. While trying to explain this better, I thought of a new way to state it. I need to select all CA that do not have a signoff record where IsCurrentQtr is true.

I need to select all CA that do not have a signoff record where IsCurrentQtr is true.

Then your query should be as follows:

var caList = db.CAs.Where(ca => ca.SignOffs.All(sf => sf.IsCurrentQtr == false)).ToList();

// or

var caList = db.CAs.Where(ca => ca.SignOffs.Count(sf => sf.IsCurrentQtr) == 0).ToList();

According to your edit, you want this result.

var result = cs.Where(x => !x.Any(r => r.IsCurrentQtr));

This will find all CA that do not have a Signoff Record where IsCurrentQtr == true.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM