[英]Querying with EF6 using potentially empty Lists
I have a function that takes 3 string[]
, and then using EF6 queries my database to try to find a match. 我有一个使用3 string[]
的函数,然后使用EF6查询我的数据库以尝试找到匹配项。 The lists can have nothing, one, or many variables in them. 列表中可以没有任何变量,一个或多个变量。
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();
}
When attempting this query, I get zero results back. 尝试此查询时,我得到的结果为零。
How can I write this so that the 3 lists can have any composition, including being empty, and I get the results I desire? 我该怎么写,以便3个列表可以有任何组合,包括为空,并且得到想要的结果?
Per Jeroen Heiers comment, I was able to break up the query by checking if the string[]
was null, and creating my query in parts. 根据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();
}
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.