简体   繁体   中英

Write where condition inside Include in linq query

The below query is returning error:

The property expression is not valid. The expression should represent a property access: 't => t.MyProperty'.

var list = this.DataContext
    .GetDbSet<Table1>()
    .Include(t => t.Table2.Where(i=>i.Active == true))
    .Include("Table2.Table3")
    .ToListAsync();

How can achieve the above query?

Filter cannot work in include. You need to download Z.EntityFramework.Plus.EFCore from nuget. And use code below

var list = this.DataContext
.GetDbSet<Table1>()
.IncludeFilter(t => t.Table2.Where(i=>i.Active == true))
.ThenInclude(x => x.Table3)
.ToListAsync();

Alternative one

var list = this.DataContext
.GetDbSet<Table1>()
.Include(t => t.Table2)
.ThenInclude(x => x.Table3)
.ToListAsync();
list.Table2 = list.Table2.Where(x => x.Active == 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