score:3
Reflection isn't a problem here; EF won't even be able to notice the difference. The delegate approach is a non-starter, by the way (since you mention EF); ultimately, it is something like:
public static IQueryable<T> Where<T>(this IQueryable<T> query,
string propertyName, object value)
{
PropertyInfo prop = typeof(T).GetProperty(propertyName);
var param = Expression.Parameter(typeof(T), "x");
var body = Expression.Equal(
Expression.Property(param, prop),
Expression.Constant(value, prop.PropertyType)
);
var predicate = Expression.Lambda<Func<T, bool>>(body, param);
return query.Where(predicate);
}
Note that you can make it easier with Expression.PropertyOrField(propertyName)
; the reason I haven't used that here is that it is very handy to know the member-type (prop.PropertyType
) when creating the constant - otherwise you can get problems with nulls.
score:0
I know this an old answer but if someone see's this I've built this project:
https://github.com/PoweredSoft/DynamicLinq
Which should be downloadable on nuget as well:
https://www.nuget.org/packages/PoweredSoft.DynamicLinq
and you could simply do
query.Where("FirstName", ConditionOperators.Equal, "David");
Source: stackoverflow.com
Related Query
- Generate Expression<> for filtering by a arbitrary property
- Combine property selector expression tree and value to create a predicate for EF filtering - create filter from lambda selector and value
- Creating expression tree for accessing a Generic type's property
- How to reuse a linq expression for 'Where' when using multiple source tables
- Expression to get LINQ with Contains to EF for SQL IN() where on entities child's property equals value
- Dynamic LINQ Expression for sorting navigation property
- what is the Linq expression tree for setting a property of an object?
- Lambda Expression for Many to Many realtionship in C# EF 5 Code First
- Repository taking linq expression for filtering
- Filtering an array of objects to remove the ones that don't have the greatest value of for a property
- using where clause inside include generate error "The Include path expression must refer to a navigation property defined on the type"
- How to get PEX to automatically generate inputs for code involving LINQ
- Dynamic Lambda Expression For Filtering
- Linq dynamic expression for filtering navigation properties and collections
- source code for LINQ 101 samples
- Use C# Expression Evaluator for filtering a list
- ForEach loop with Lambda expression in Razor code MVC 5 For List<T>
- Build LINQ Lambda Expression with contains method for enum property with list of int
- Dynamic lambda expression for array property filter
- Dynamic LINQ OrderBy for a Navigation Property using Expression in C#
- Saved projection expression for re-use in different linq expressions with two source objects
- getting at a deep property for a lambda Expression
- Building lambda expression for filtering in list of dictionaries
- Dynamic linq expression for filtering with enum collection
- Retrieving Property name from lambda expression
- C# - code to order by a property using the property name as a string
- Expression trees for dummies?
- Construct LambdaExpression for nested property from string
- Basic LINQ expression for an ItemCollection
- How does LINQ expression syntax work with Include() for eager loading
More Query from same tag
- Dynamic LINQ. No property or field 'FieldName' exists in type 'ClassName'
- Multiple randomly selection Linq
- how to get and use the index of each list member
- Why doesn't the Controls collection provide all of the IEnumerable methods?
- How to Optimize below linq query
- Select items from linear list into list with subitems
- LINQ Order by another property is the first property is equal to a given value
- The cast to value type 'Decimal' failed because the materialized value is null
- SQL expressions in EF using Lambda
- How to convert List<string> to Dictionary<string,string> using linq?
- C# / LINQ - conditional LINQ query based on checkbox status
- Casting generic type to build a valid Linq expression
- LINQ to XML can't get element
- Datatable C# - How to get detail record from one table and its associated sub record from another table based on ID
- Getting the number of elements that are the same at the same index
- LINQ merge a collection
- c# Collapse and flatten list of nested payments in object list
- Lightswitch 2013 Linq queries to Get min value
- Selecting specific columns from GroupBy list
- Initializing var type in LINQ
- Linq : Where + keeping order
- Entity Framework Select with Include + Group By Count + Paging and Projection
- Linq not equal to returning all values in wpf datagrid
- Using List.Find or LINQ on lists of enums in .NET 3.5
- LINQ Quicksort is Unstable Except When Cascading
- Get specific column header names from DataTable
- Entity Framework "Could not find an implementation of the query pattern"
- MongoDB: remove item from child collection
- LINQ Group elements that are equal together
- Read XML to Datatable dynamically