expression-trees

Convert Expression trees

let there be : Expression<Func<Message, bool>> exp1 = x => x.mesID == 1; Expression<Func<MessageDTO, bool>> exp2 = x => x.mesID == 1; now i need to pass exp1 to _db.Messages.where(exp1); problem is i only have exp2, i need to convert the type to Message , All properties are the same ! now i do this : var par = Expression.Parameter...

Convert an expression tree for a c# query expression

I am having a problem converting this query via an expression tree: WageConstIn => Convert.ToString(WageConstIn.Serialno).StartsWith("2800") This is my expression tree: var searchTextExp = LinqExpression.Constant("2800"); var parameterExp = LinqExpression.Parameter(typeof(WageConstInEntity), "WageConstIn"); var propertyExp = LinqE...

IQueryable: Creating dynamically an OR filtering

I have a set of search criterias in this form: member | value | operator --------+---------+--------- height | 10 | > height | 2 | < name | Carl | == And I want to query all the objects that match any of these criterias. Right now, I'm doing it by: building an expression for each one of the crit...

What does Expression.Quote() do that Expression.Constant() can’t already do?

Note: I am aware of the earlier question “What is the purpose of LINQ's Expression.Quote method?”, but if you read on you will see that it doesn’t answer my question. I understand what the stated purpose of Expression.Quote() is. However, Expression.Constant() can be used for the same purpose (in addition to all the purposes that Expres...

Expression Tree Creation and ExpressionTree Convert Type

lets say i have : anything.where(x=>x.age == int.parse(txtage.text)); now i know that int.parse(txtage.text) is an expression of type ExpressionType.Convert now i wanna know how to create an expression of type ExpressionType.Convert manually (programatically) why ? because im passing expressions between layers and changing the ty...

MetaLinq vs MSDN's ExpressionTreeSerialization

Hi, There are two available solutions (that I could find) that have the ability to serialize expression trees: MetaLinq which has some other awesome features Expression Tree Serialization on MSDN Code Gallery which seems to be more lightweight I have little experience with these tools, so I can't decide whichever is the better. I'm ...

How to use Expression to build an Anonymous Type?

In C# 3.0 you can use Expression to create a class with the following syntax: var exp = Expression.New(typeof(MyClass)); var lambda = LambdaExpression.Lambda(exp); object myObj = lambda.Compile().DynamicInvoke(); But how do you use Expression to create an Anonymous class? //anonymousType = typeof(new{ Name="abc", Num=123}); Type anon...

How do I dynamically construct a predicate method from an expression tree?

Here's the scenario: Silverlight 4.0, DataGrid, PagedCollectionView itemssource. The objective is to apply a Filter to the PCV. The filter needs to be a Predicate<object>(Method) - where Method implements some logic against the object and returns true/false for inclusion. What I have is a need to optionally include 3 different criteria ...

How would I use a linq expression to create a delegate with parameters?

Hi, I found your code example below extremely helpful, however I don't understand the use of expressions in the code... I am looking to modify this method to accept a delegate with a params object[] parameter.. any pointers would be greatly appreciated. Simon Bridge ([email protected]) class EventProxy { static public Delegate C...

Expression Trees as the basis for user defined query engine (using EAV)?

I'm supporting a multi-tenant system that allows users to define custom forms. The data model is EAV based because issuing dynamic DDL to change table schema doesn't work when supporting multiple tenants in a single DB. An upcoming requirement is to build a flexible query designer that allows users to setup simple predicates against th...

Compiled LinQ query with with expressions in functions

I would like to create a compiled query which uses reusable where predicates. An example to make this clear: ObjectContext.Employees.Where(EmployeePredicates.CustomerPredicate) EmployeePredicates is a static class with a property CustomerPredicate that looks like this: public static Expression<Func<Employee, bool>> CustomerPredicate ...

Linq: Dynamic Query Contruction: query moves to client-side

Hi all, I've been following with great interest the converstaion here: http://stackoverflow.com/questions/3769141/construct-query-with-linq-rather-than-sql-strings with regards to constructing expression trees where even the table name is dynamic. Toward that end, I've created a Extension method, addWhere, that looks like: static pub...

Injecting a variable into the Mono.CSharp.Evaluator (runtime compiling a LINQ query from string)

I'm using the Mono.CSharp library to emit code. Following another question on SO (http://stackoverflow.com/questions/3407318/mono-compiler-as-a-service-mcs) I managed to get Mono.CSharp evaluating correctly on the Microsoft CLR. To add flexibility in my app I'd like to be able to customize a query at runtime - by allowing the user to p...

Expression Tree Dependency Analyzer

I'm building an expression tree dependency analyzer for a cross data source IQueryProvider. That is, I have an IQueryable with some elements that can be executed locally in memory against some arbitrary provider (say Entity Framework). Some other elements in the IQueryable go against an entity that I need to make a remote WCF call. The ...

Reverse of Expression<Func<T,TResult>>.Compile()?

Since we can: Expression<Func<int, bool>> predicate = x => x > 5; var result = Enumerable.Range(0,10).Where(predicate.Compile()); How can I: Func<int,bool> predicate = x => x > 5; Expression<Func<int,bool>> exp = predicate.Decompile(); That is, I want to get the corresponding Expression of the Func. Is it possible? ...

Why can't I create the same Expression Tree manually that my straight lambda produces

I've gone through and beat my head against the wall for a while now searched on various phrases and keywords but I cannot find anything close to an answer so i'm hoping someone here can shed some light. Basically I'm working on diving pretty deep into manipulating, creating, and modifying Expression Trees in C# 4.0 I came across an odd...

Expression.Call and "Ambiguous Match Found"

I'm trying to write an expression that will call ToString on a property and assign it's value to a local variable. However, calling ToString on a object instance w/ an overload of ToString, causes an exception of "Ambigous Match Found" to be thrown. Here's an example: var result = Expression.Variable(typeof(string), "result"); var match...

Working with nullable types in Expression Trees

I have an extension method to dynamically filter Linq to Entities results using string values. It works fine until I use it to filter nullable columns. Here's my code: public static IOrderedQueryable<T> OrderingHelperWhere<T>(this IQueryable<T> source, string columnName, object value) { ParameterExpression table = Expression.Paramet...

Dynamically Adding a GroupBy to a Lamba Expression

Hi guys, Ok, I'll admit that I don't entirely "get" lamda expressions and LINQ expression trees yet; a lot of what I'm doing is cutting and pasting and seeing what works. I've looked over lots of documentation, but I still haven't found the my "aha" moment yet. With that being said... I'm attempting to dynamically add a GroupBy express...

ExpressionTree - GetSetMethod Error: Method 'System.String get_Name()' is not defined for type 'System.String'

Here is my method: public static MethodCallExpression ClonePropertyAssignmentLambda<T>(Expression source, string property) { var targetExp = Expression.Parameter(typeof (T), "target"); var propertyInfo = typeof (T).GetProperty(property); var targetProperty = Expression.Property(targetExp, propertyInfo); ...