Write where condition inside Include in linq query

c# entity-framework entity-framework-core linq sql-server

Question

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?

1
0
1/23/2020 12:42:49 PM

Accepted Answer

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); 
3
1/23/2020 7:28:48 AM


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