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.
Source: stackoverflow.com
Related Query
- How can I refactor this code for LINQ filtering?
- How can I combine this code into one or two LINQ queries?
- How can I further simplify this piece of LINQ code
- How can i optimize this linq function for calculating sum of "OrePreviste"?
- How can I check for null values in this linq query where clause
- How can I check for a NullReferenceException in this C# LINQ to XML statement?
- how to write LINQ to objects equivalent code for this code
- How can one refactor this Linq function?
- How can I simplify this LINQ query that searches for keywords in strings and orders them by relevance?
- How can I simplify this LINQ code
- How can I optimize this LINQ statement for speed?
- How can i convert this code snippet into LINQ format?
- Can someone help me refactor this C# linq business logic for efficiency?
- How can I get LINQ to return the object which has the max value for a given property?
- How to refactor this duplicated LINQ code?
- How can I convert this SQL Query into LINQ (OVER (PARTITION BY Date))
- How do I write this in Ruby/Python? Or, can you translate my LINQ to Ruby/Python?
- How can I use LINQ to project this parent and children object model into a flat, single object?
- How to reuse a linq expression for 'Where' when using multiple source tables
- How can I switch that code to use LINQ
- How does this linq code that splits a sequence work?
- How can I dynamically store expressions used for Linq Orderby?
- How can I GroupBy this LINQ query?
- linq - how do you do a query for items in one query source that are not in another one?
- How can i use linq for parsing string datetime to real datetime?
- How can I add OrderBy to this LINQ statement?
- How can I write the following code more elegantly using LINQ query syntax?
- Can I use an anonymous type for this Linq grouping?
- How can I check for a null inside of a linq query?
- How can I code an outer join using LINQ and EF6?
More Query from same tag
- Changing variable in LINQ select
- Getting multiple count in EF Model LINQ in single DB call
- Looking for LINQ syntax that gets the second item in a list that starts with something specific
- Create multiple DataTable rows based on a column with comma separated values
- LINQ understading Non-Equijoins
- How can I combine two LINQ select expressions?
- VB.NET LINQ Query - List rows rows in table1 where data has changed
- IEnumerable return First
- How do I display total runtime in SqlServer Management Studio 2005?
- sort list with C# by how often same strings appear in list
- In Linq2SQL, how do I get a record plus the previous and next in the sequence in a single query?
- C# XML parsing with LINQ storing directly to a struct?
- Using except operator with property condition
- Return one Element from List using LINQ
- How to filter IQueryable by a new field
- Fetch Entity Framework objects with partially loaded collections
- better way to do this foreach using linq
- LINQ in Razor function
- How to sum Timespan of subtraction between two datetimes in Linq when using group by?
- How cast System.Data.Entity.Infrastructure.DbQuery type to System.Collections.Generic.List type?
- Guid compare not working on linq where condition
- Resolve Linq.Expressions.NewExpression?
- search decimal values in Linq query
- Odd Linq behavior with IList / IEnumerable
- How can I create a method that will print LINQ query results?
- How to concat two lists in Linq when the underlying anonymous types are similar?
- String or binary data would be truncated in Linq
- Lambda Expression of Select * from TableName
- Linq query to get count with multiple column values coming from condition on another column
- Transform Dictionary<string, int> to Dictionary<int, List<string>>