所以,我有財產,這是一個表達。
public Expression<Func<Profile, bool>> ManagerFilter { get; set; }
接下來,我要實作這個過濾器,也就是這里動態的上面的運算式:
var queryTest = applicantCacheRepo
.Include(a=>a.Profile)
.ThenInclude(p=>p.ProfileEmployer)
.ThenInclude(p=>p.Employer)
.Include(a=>a.ProfileApplicationDetail)
.ThenInclude(p=>p.ApplicationStatusSysCodeUnique)
.Include(a=>a.Person)
.ThenInclude(p=>p.PersonDetail)
.Include(a=>a.JobSpecification)
.ThenInclude(j=>j.JobSpecificationDetail)
.FirstOrDefaultAsync(a=>a.Profile == filters.ManagerFilter)
我在這里嘗試做的是在此查詢中動態應用Profile過濾器。問題是我不知道Profile的確切屬性,將根據該屬性對其進行過濾。
問題是:我怎樣才能在這里動態實作這個過濾器?
uj5u.com熱心網友回復:
您只能在第三方庫的幫助下做到這一點。我建議使用LINQKit。它只需要配置DbContextOptions:
builder
.UseSqlServer(connectionString) // or any other provider
.WithExpressionExpanding(); // enabling LINQKit extension
然后你可以通過Invoke擴展使用你的過濾器:
var queryTest = await
...
.FirstOrDefaultAsync(a => filters.ManagerFilter.Invoke(a.Profile));
轉載請註明出處,本文鏈接:https://www.uj5u.com/gongcheng/478168.html
