Reputation: 73
I am trying to create a search function that will read from the database to look through multiple columns and return if all keywords (delimited by space) have been used.
Currently I have something a long the lines of
db.Persons.Where(
q=>
(keywords.Count() > 0)
||
(
keywords.Any(k => k.firstName.ToUpper().Contains(k)
||
keywords.Any(k => k.lastName.ToUpper().Contains(k)
||
keywords.Any(k => k.address.ToUpper().Contains(k)
)
)
However, I would like it to make sure that each keyword is in at least one column. Is this possible in LINQ or do I need to loop through it externally?
Upvotes: 0
Views: 371
Reputation: 19526
How about something like:
db.Persons.Where(q =>
{
string[] values = new string[] { q.firstName, q.lastName, q.address };
return keywords.All(k => values.Any(v => v.ToUpper().Contains(k));
});
Upvotes: 3