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 Articles
- 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
- using where clause inside include generate error "The Include path expression must refer to a navigation property defined on the type"
- Retrieving Property name from lambda expression
- C# - code to order by a property using the property name as a string
- Generate EF orderby expression by string
- LINQ Expression to return Property value?
- LinqKit System.InvalidCastException When Invoking method-provided expression on member property
- The Include path expression must refer to a navigation property defined on the type.in eager loading
- Using a LINQ ExpressionVisitor to replace primitive parameters with property references in a lambda expression
- How to convert PropertyInfo to property expression and use it to invoke generic method?
- Building an OrderBy Lambda expression based on child entity's property
- passing dynamic expression to order by in code first EF repository
- Creating expression tree for accessing a Generic type's property
- Using LINQ expression to assign to an object's property
- Creating a property selector Expression from a string
- Store multi-type OrderBy expression as a property
- How to reuse a linq expression for 'Where' when using multiple source tables
- Expression tree to initialize new anonymous object with arbitrary number of properties
- How to get property name from expression
- How to generate dynamic expression with a bitwise operator and enums?
- every Parameter object property that is not null, to be added to expression predicate as a condition
- 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
- Forcing Entity Framework to not generate NCLOB's when building Linq-to-Sql Code (Model First)
- Execute expression on another IQueryable source
- LINQ Source Code Available
- Access property in lambda expression from string when using LINQ
- what is the Linq expression tree for setting a property of an object?
- Accessing nested property from a collection in an Expression
- How to check Entity's created date is 20 days old or not?
- Linq issues querying two Collections of data
- EnumHelper.Parse in LINQ JOIN Query
- c# Linq select join on select group by
- Linq: Group the results of several GroupBy
- Linq to Entities : Count is very slow
- selecting multiple columns and looping through the selected rows
- select distinct parent with multiple children
- Regex in Linq (EntityFramework), String processing in database
- CS8602 – Possible null reference – How to resolve properly this warning?
- Array Initialization ways
- ASP.NET Core Returning
- Call Generic Method with an anonymous Type (C#)
- Parallel.ForEach freezes after some time
- xml Append, File.Add... overwrites original data
- Table into Tree Structure c#
- Returning the wrong type - cast?
- Why is this Linq to XML query not working
- Column value can't duplicate
- C# LINQ Debugging through an IEnumerable extension method