I'm implementing a basic search service, and several different repositories are injected into it. The repositories have a method that allows for an expression to be used like so:
public IEnumerable<T> Select(Expression<Func<T, bool>> predicate)
{
return _dbSet.Where(predicate).AsEnumerable().ToList();
}
I currently have a statement like this in my search service:
searchResult.FoundCustomers = _customerRepository.Select(x =>
x.FirstName.StartsWith(searchString) ||
x.LastName.StartsWith(searchString) ||
x.City.StartsWith(searchString) ||
x.Country.StartsWith(searchString) ||
x.Phone.StartsWith(searchString) || x.Phone.EndsWith(searchString)).ToList();
Is there a way of improving the LINQ? The repetitiveness of searchString
seems unnecessary, but I don't yet know LINQ well enough to know if there is a way of avoiding it.