How can I define an array inside of a dynamic linq expression string, that I can pass into .Where(string)
?
I already tried { "val a", "val b", "val c" }
, but this does not work.
Here's an example of what I want to achieve:
public class Person
{
String Name { get; set; }
int Age { get; set; }
}
Now I want to query an IQueryable<Person>
using a completely dynamically created dynamic linq expression string, where e.g. Name
is queried against a list of predefined values. The following works, but is cumbersome when there are a lot of values:
myQueryablePersons.Where("Name == \"John Doe\" || Name == \"Mr Smith\" || Name == \"CatWoman\");
What I'd like to do is something like this:
myQueryablePersons.Where("{ \"John Doe\", \"Mr Smith\", \"CatWoman\" }.Contains(Name)");
I know how this can achieved using plain C# or plain Linq, but I want to do it by only using the string passed into Where()
. Is this possible at all? I already searched other answers on Stackoverflow and other sites, but could not find any answer that really fits my specific question.