expression-trees

Extract the lambda from an Expression?

I want to implement this function: Public Function GetFunc(Of TSource, TResult) _ selector As Expression(Of Func(Of TSource, TResult)) _ As Func(Of TSource, TResult) 'Implement here End Function UPDATE I have the following issue, it's a part of a function: Dim obj As Object = value For Each exp In expressions If ob...

Create fully dynamic where clause with expression tree and execute on IQueryable

At point (3) in my code I have defined a query called query1 in which I defined a .Where lambda expression. This query is in some way dynamic but still contains static elements, it always refers to the Type Employee and its (int) property ClientID. Now I very much like to make the refering to the type and its property dynamic, based on...

Getting Values from ExpressionTrees

let there be: Expression<Func<Customer, bool>> expression = c => c.Name == "John"; now i get the value by using : string myvalue = ((ConstantExpression) bin.Right).Value; now let there be: string x = "John"; Expression<Func<Customer, bool>> expression = c => c.Name == x; now i understand that string myvalue = ((ConstantExpress...

How can I convert the name of a strongly typed resource to an Uri

In my programs I prefer using strongly typed resources instead of strings. For instance I have an image resource called: Properties.Resources.Resize. I'm using a library that only allows an Uri of this resource to add an image to a button, like this: LargeImage = new Uri( @"/Resources/Resize.png", UriKind.Relative); To archieve this u...

Querying Entity with LINQ using Dyanmic Field Name

I have created a dynamic search screen in ASP.NET MVC. I retrieved the field names from the entity through reflection so that I could allow the user to choose which fields they wanted to search on instead of displaying all fields in the view. When the search result is Posted back to the controller, I receive a FormCollection containing...

Expression Trees Conversion

I have two Classes : class Customer { public string Fullname { get; set; } public string Lastname { get; set; } public int Age { get; set; } } and class CustomerDTO { public string Fullname { get; set; } public string Lastname { get; set; } public int Age { get; set; } } now i have an expressiontree Expressi...

Convert func to predicate using reflection in C#

I'm basically trying to do this, but I don't know what T will be, so I'm building things up using Reflection and Expression trees. // Input (I don't know about "Book") Type itemType = typeof(Book); // Actual Code // Build up func p => p.AuthorName == "Jon Skeet" ParameterExpression predParam = Expression.Parameter(itemType, "p"); Expre...

Passing an expression tree as a parameter to another expression tree

I have two expression trees defined like this: private Expression<Func<TEntity, TPropertyResult>> PropertyAccessor { get; set; } and private Expression<Func<TPropertyResult, bool>> TestExpression { get; set; } I need to create a new expression tree that will result in the equivalent of: var expression = p => this.TestExpression(th...

BinaryExpression to Lambda

This may be familiar to some. I have a wrapper class Ex that wraps an expression tree with a bunch of implicit conversions and operators. Here is simplified version public class Ex { Expression expr; public Ex(Expression expr) { this.expr = expr; } public static implicit operator Expression(Ex rhs) { return...

Expression trees: Getting the statements from a MethodCallExpression

I had some problems figuring out a good title, but hopefully the code examples in this post are clear enough. Is it possible, with the help of expression trees (or some other technique), to traverse the whole method "call stack"? Worded differently: When I get an expression tree from an Action-delegate, I would like to traverse inside ...

In C#, why Expression Trees and when do you need to use them?

When I need the Expression Trees ? And please provide us with a real world sample if available Thanks in advance ...

Evaluating an expression stored as a string

I want to store a boolean expression in a database, and evaluate it. It’s not necessary to store the complete expression tree, a string probably do it. I imagined a scheme like this: Criteria (expression_to_evaluate, value_to_return) For example, let’s say that I have an expression that matches people with age between 20 and 40: (var ...

C# 4 "dynamic" in expression trees

I'm trying to figure out how to put all the pieces together, and would appreciate a concrete source code sample for a simple case to start with. Consider the following C# code: Func<int, int, int> f = (x, y) => x + y; I can produce an equivalent function at runtime using expression trees as follows: var x = Expression.Parameter(type...

Is is feasible to try to convert expression trees between business and data domains?

I have a repository layer that deals with LINQ to SQL autogenerated entities. These eventually get mapped into domain-friendly types on the surface. I'd now like to provide some more sophisticated querying capabilities for the client code, and that client code knows only about the domain object types. I'd like to implement this with t...

Can I accept both a delegate type T and an Expression<T> in the same parameter?

I'm trying to write a helper class that represents fields on an object. The helper class needs to be able to both get the value of the field on a given instance AND return metadata about the underlying property that it can obtain by reflection. I'd like the helper class be created by a utility method that gets called something like the ...

Traverse a Linq Expression to set the value of a property field

This is a very complicated question even though it looks simple. I think I would have to traverse the entire expression tree unless someone knows a better way. Let's say I have a user object class User { public UserAccount Account {get;set;} } class UserAccount { public Name {get;set;} } var user = new User() { Account = new Us...

How do I convert an Enum to an Int for use in an Expression.Equals operation?

I am trying to dynamically build an expression tree in C#, which is compiled and used as the predicate for LINQ-to-SQL Where() call. The problem is that I am trying to compare an Enum (with int as its underlying type) directly against an Int, but this is failing with the error "The member MyEnumType has no supported translation to SQL". ...

How to convert Expression<Func<T, TProperty>> to Expression<Func<T, TNewProperty>>

The following is working, but my Body.NodeType changes to Convert, instead of MemberAccess which is a problem when getting ModelMetadata.FromLambdaExpression: private Expression<Func<TModel, TNewProperty>> ConvertExpression<TProperty, TNewProperty>(Expression<Func<TModel, TProperty>> expression) { Exression converted = Expression.Co...

.NET 4.0: Parameter naming inconsistency between Expression and MethodInfo

Hello there, I have just found some misterious behaviour while working with System.Linq.Expressions.Expression and System.Reflection.MethodInfo. The code is as follows: static void Main(string[] args) { Expression<Func<double, double, double>> example = (x, y) => Math.Sin(x); //Prints out "x, y": Cons...

Create an expression tree in c#

I am attempting to create a dynamic query using expression trees in LINQ to represent the following query WageConstIns.Where(WageConstIn => WageConstIn.Serialno.ToString().StartsWith("2800")); I have attempted to create it like so: MemberExpression le1 = LinqExpression.Property(paramExp, "Serialno"); MethodCallExpression le2 = LinqEx...