Linq to SQL Where clause based on field selected at runtime
Posted
by
robasaurus
on Stack Overflow
See other posts from Stack Overflow
or by robasaurus
Published on 2013-11-04T15:51:09Z
Indexed on
2013/11/04
15:53 UTC
Read the original article
Hit count: 304
I'm trying to create a simple reusable search using LINQ to SQL.
I pass in a list of words entered in a search box. The results are then filtered based on this criteria.
private IQueryable<User> BasicNameSearch(IQueryable<User> usersToSearch, ICollection<string> individualWordsFromSearch)
{
return usersToSearch
.Where(user =>
individualWordsFromSearch.Contains(user.Forename.ToLower())
|| individualWordsFromSearch.Contains(user.Surname.ToLower()));
}
Now I want this same search functionality on a different datasource and want to dynamically select the fields to apply the search to. For instance instead of IQueryable of Users I may have an IQueryable of Cars and instead of firstname and surname the search goes off Make and Model. Basically the goal is to reuse the search logic by dynamically selecting what to search on at runtime.
© Stack Overflow or respective owner