score:3

you can extract this method:

public bool searchformatching(string source, string serach)
{
    return source.tolower().contains(search.tolower());
} 

it will simplify your where clause:

where(x => searchformatching(x.id, search)
           || searchformatching(x.firstname, search)
           || searchformatching(x.lastname, search)
           || searchformatching(x.description, search));

another option if you don't want to search for every property one by one and you want to check if any of the class's propeties answers the search is to use reflection to iterate over the class's properties and check if there is any one that answers the condition:

public bool searchformatching(user user, string search)
{
    return user.gettype().getpropeties().any(propertyinfo => propertyinfo.getvalue(user).tostring().tolower().contains(search.tolower()));
}

then use this method in the where clause:

where(x => searchformatching(x, search));

or just combine them together:

where(x => x.gettype().getpropeties().any(propertyinfo => propertyinfo.getvalue(x).tostring().tolower().contains(search.tolower())); 

edit
the two options should work just fine with linq to objects but it will probably not collaborate well with linq to entities because it is not possible to translate neither the first option or the reflection option to sql using linq to entities.

you can load all the data to memeory using db.user.asenumerable() and then work with linq to objects with any of these options, but it is less efficient than doing all the filtering in the database like your first query does, i suggest you despite it's readblity to keep your first query.


Related Query

More Query from same tag