I have a function that takes 3 string[]
, and then using EF6 queries my database to try to find a match. 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?
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.
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();
}
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.