I have two Classes:
public Case()
{
public IList<CaseThirdParty> CaseThirdParties { get; private set; }
}
public CaseThirdParty()
{
public bool Received_Claim { get; private set; }
}
I am using ef core to get all Cases but only get
CaseThirdParties
where Received_Claim
is True
, here is the query:
await _context.Cases.Include(c => c.CaseThirdParties)
.Where(c => c.CaseThirdParties.Any(c => c.Received_Claim))
.ToArrayAsync();
Or
await _context.Cases.Include(c => c.CaseThirdParties)
.Where(c => c.CaseThirdParties.Where(c => c.Received_Claim).Count>0)
.ToArrayAsync();
But it retrieves CaseThirdParties
where Received_Claim
is flase
CodePudding user response:
Everthing seems fine.
Can you specify, what exactly don`t work, there is no entities in result or logic error(unexpected entities in result)
CodePudding user response:
If you use ef core 5.0 or later you can use Filtered include
https://docs.microsoft.com/en-us/ef/core/querying/related-data/eager#filtered-include
like this:
await _context.Cases.Include(case => case.CaseThirdParties.Where(c => c.Received_Claim))
.ToArrayAsync();
if not you have to create an anonymous object with Case and CaseThirdParties like this:
await _context.Cases.Where(case => case.CaseThirdParties.Any(case3rd => case3rd.Received_Claim))
.Select(case =>
{
Case = case,
CaseThirdParties = case.CaseThirdParties.Where(c => c.Received_Claim))
}
.ToArrayAsync();