How does PredicateBuilder work

Let’s say you have: Expression<Func<Person, bool>> isAdult = p1 => p1.Age >= 18; // I’ve given the parameter a different name to allow you to differentiate. Expression<Func<Person, bool>> isMale = p2 => p2.Gender == “Male”; And then combine them with PredicateBuilder var isAdultMale = isAdult.And(isMale); What PredicateBuilder produces is an expression that looks like this: … Read more

Construct LambdaExpression for nested property from string

Do you mean: static LambdaExpression CreateExpression(Type type, string propertyName) { var param = Expression.Parameter(type, “x”); Expression body = param; foreach (var member in propertyName.Split(‘.’)) { body = Expression.PropertyOrField(body, member); } return Expression.Lambda(body, param); } For example: class Foo { public Bar myBar { get; set; } } class Bar { public string name { get; … Read more

How set value a property selector Expression

This works: The following helper method converts a getter expression into a setter delegate. If you want to return an Expression<Action<T,TProperty>> instead of an Action<T,TProperty>, just don’t call the Compile() method at the end. Note: The code is from Ian Mercer’s blog: http://blog.abodit.com/2011/09/convert-a-property-getter-to-a-setter/ /// <summary> /// Convert a lambda expression for a getter into a … Read more

variable ” of type ” referenced from scope ”, but it is not defined

As indicated in the other answer, you have two expressions where both have a parameter named y. Those don’t automatically relate to each other. To properly compile your expression, you need to specify both source expression’s parameters: Expression<Func<string, bool>> e1 = (y => y.Length > 0); Expression<Func<string, bool>> e2 = (y => y.Length < 5); … Read more

How do I create an expression tree calling IEnumerable.Any(…)?

There are several things wrong with how you’re going about it. You’re mixing abstraction levels. The T parameter to GetAnyExpression<T> could be different to the type parameter used to instantiate propertyExp.Type. The T type parameter is one step closer in the abstraction stack to compile time – unless you’re calling GetAnyExpression<T> via reflection, it will … Read more

Internal .NET Framework Data Provider error 1025

While the other answers are true, note that when trying to use it after a select statement one has to call AsQueryable() explicitly, otherwise the compiler will assume that we are trying to use IEnumerable methods, which expect a Func and not Expression<Func>. This was probably the issue of the original poster, as otherwise the … Read more

Generate EF orderby expression by string [duplicate]

Using reflection and expression-trees you can provide the parameters and then call OrderBy function, Instead of returning Expression<Func<Task, T>> and then calling OrderBy. Note that OrderBy is an extension method and has implemented in both System.Linq.Enumarable and System.Linq.Queryable classes. The first one is for linq-to-objects and the latter is for linq-to-entities. entity-framework needs the expression … Read more