I'm trying to write a Linq query which fetches all users whose first or last name begins with at least one string in a list of strings. This is used for auto-completion of recipients in a messaging system.
This was my first naive attempt:
var users = UserRepository.ALL()
foreach (var part in new [] { 'Ha', 'Ho', 'He' })
{
string part1 = part; // "Copy" since we're coding lazily
users = users.Where(x => x.LastName.StartsWith(part1) ||
x.FirstName.StartsWith(part1));
}
This doesn't work though, as the result becomes:
users.Where(a || b).Where(c || d).Where(e || f)...
...whereas I want:
users.Where(a || b || c || d || e || f || ...)
How would I go about doing this?