我有一个需要3 string[]
的函数,然后使用EF6查询我的数据库以尝试查找匹配项。列表中可以包含任何内容,一个或多个变量。
using (var db= new Db(ConnectionString))
{
var results =
await
db.dbases.Where(
w =>
portfolioSelected.Any(a => a == w.portfolio) &&
statusSelected.Any(a => a == w.statusname) &&
deskSelected.Any(a => a == w.assignedto)
).ToListAsync();
}
尝试此查询时,我得到零结果。
我怎么写这个以便3个列表可以有任何组合,包括空,我得到我想要的结果?
答案 0 :(得分:0)
根据Jeroen Heiers评论,我能够通过检查string[]
是否为空来分解查询,并在部分中创建我的查询。
using (var db = new Db(ConnectionString))
{
IQueryable<dbase> query = cmax.dbases.Where(w => w != null);
var results = new List<dbase>();
if (portfolioSelected != null)
query = query.Where(w => portfolioSelected.Any(a => a == w.portfolio));
if (statusSelected != null)
query = query.Where(w => statusSelected.Any(a => a == w.statusname));
if (deskSelected != null)
query = query.Where(w => deskSelected.Any(a => a == w.assignedto)); break;
results = await query.ToListAsync();
}