The following code:
var dynamicQuery = from a in _context.Users select a;
string[] args = new string[] { "aa", "bb", "cc" };
foreach (string word in args)
{
dynamicQuery = dynamicQuery.Where(x => x.Name.Contains(word));
}
return dynamicQuery.ToList();
Will allow me to create a LINQ query with a dynamic list of AND expressions.
But suppose I wanted to do the same, only with a dynamic list of OR expressions?
You don’t need to loop at all:
EDIT: More generally, you can use:
This will end up with quite deep stacks (with one delegate calling another calling another etc). Where the specific situation allows you to use
Any, that would usually be a better approach.I would expect
Anyto work in most situations where you’ve got a collection of items to potentially match against… the non-Anyapproach is approprate for “in some situations, anyone over 18 is fine… in some situations anyone with a last name beginning with “G” is appropriate, etc.