EFCore Linq Left Outer Join

c# entity-framework-core linq sql-server

Question

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.

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.

1
1
2/7/2019 5:45:02 PM

Accepted Answer

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();
2
2/7/2019 6:27:56 PM

Popular Answer

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.



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