I've got an Objectset that represents a set of people and I want to use it as the DataSource for a DataGridView control. But before that, I want to filter the set of people based on a search string. So if a search string were "David John" I would want to keep "David Johnson" and "John Davidson", but not "John Williams", "David Beckham" or "Al Green".
Here's what I've tried:
MyObjectContext context = GetContext();
string searchBox = "John David";
Regex regex = new Regex("[a-zA-Z]+", RegexOptions.Singleline);
MatchCollection matches = regex.Matches(searchBox);
IQueryable<Owner> q = ce.Owner;
foreach (Match match in matches)
{
q = q.Where(o => o.FirstName.Contains(match.Value)
|| o.LastName.Contains(match.Value));
}
findOwnerDataGrid.DataSource = q.OrderBy(o => o.LastName);
but it only seems to apply the last match "David".
How can I accomplish what I need to do? Any different or simpler solutions are welcome. If it matters, the grid is read-only so I don't have to worry about binding/editing considerations.