Reputation: 670
At runtime I have a dynamic list of "Parent" ids i need to query against one of my entity tables, using LINQ how would I generate the query at run time? Currently am (VERY SLOWLY) looping over my list of ID's and performing a separate query to the database using the Where method. Is there an API that will allow me to build a single WHERE statement that in essence is appending "&& value = something" as I would have if I was writing the SQL myself?
Currently what i do looks like this
foreach(var parent in parents)
{
col.AddRange(context.LocalAuthorities.Where(c => c.Parent.ID == parent.ID).ToList());
}
Upvotes: 0
Views: 53
Reputation: 978
If your goal is to replace multiple iterative queries by one query, you can use this approach:
var allParentIds = parents.Select(p=>p.ID).ToList();
var result = context.LocalAuthorities.Where(c=> allParentIds.Contains(c.Parent.ID));
col.AddRange(result);
Upvotes: 1