Entity Framework Code First is what I'm utilizing. I want to search database entities against List objects. With contains, this works well, but how can I combine it with StartsWith?
Here's my code:
List<string> values = new List<string>(); values.Add("A"); values.Add("B"); context.Customer.Where(c => values.Contains(c.Name)).ToList();
How can I run a query on every client whose name begins with A or B?
This ought to function in memory, but I'm not sure whether EF could convert it to SQL:
context.Customer.Where(c => values.Any(s => c.Name.StartsWith(s))).ToList();
Since it plainly includes A or B if it begins with A or B, you don't need to combine it with StartsWith. It must contain A or B in order for it to begin with A or B.
Therefore, just substitute StartsWith for Contains.
context.Customer.Where(c => c.StartsWith("A") || c.StartsWith("B")).ToList();