The entities (simplified) are as follows:
class A
{
public Guid ID { get;set; }
public string Name { get;set; }
public ICollection<B> Bs { get;set; }
}
class B
{
public Guid ID { get;set; }
public ICollection<C> Cs { get;set; }
}
class C
{
public Guid ID { get;set; }
public string Key { get;set; }
}
I want to query for all of class A where the Key Property of class C equals 'test'. What I tried to do is:
var as = await this._applicationDbContext.As
.Where(a=> a.Bs.Any(b => b.Cs.Any(c=> c.Key == 'test')))
.ToListAsync();
But I am not getting anything back. I know I could include Bs and then Cs and do it in code, but there should be a way to do it in the ef query?
Your query should work, anyway try the following variant:
var as = await this._applicationDbContext.As
.Where(a => a.Bs.SelectMany(b => b.Cs).Any(c => c.Key == 'test'))
.ToListAsync();
You need to use .Include()
.Where(a=> a.Bs.Any(b => b.Cs.Any(c=> c.Key == 'test'))).Include(a => a.
Bs).ThenInclude(b => b.Cs).ToListAsync();
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.