expression-trees

Practical use of expression trees

Expression trees are a nice feature, but what are its practical uses? Can they be used for some sort of code generation or metaprogramming or some such? ...

Expression.Or, The parameter 'item' is not in scope.

I am trying to write a static function to Or two expressions, but recieve the following error: The parameter 'item' is not in scope. Description: An unhandled exception occurred during the execution of the current web request. Please review the stack trace for more information about the error and where it originated in ...

In LINQ to SQL, how do you pass pass parts of a LINQ query into a function

Is it possible to pass parts of a linq Query into a function? I want create a common interface for my DAL that always uses the same query interface. For example, List<T> Get(Join j, Where w, Select s){ return currentDataContext<T>.Join(j).Where(w).Select(s).ToList(); } Is this sort of thing possible? I'm thinking it wo...

Determining scope of a MemberExpressions target

Is there anyone here with experience writing custom Linq proviers? What I'm trying to do is tell whether a MemberExpression that is a property on a business object should be included in the sql, or treated as a constant, because its from a local variable that just happens to be a business object. So for example, if you have this: Cust...

Is it possible to cast a delegate instance into a Lambda expression?

Here the context for my question: A common technique is to declare the parameter of a method as a Lambda expression rather than a delegate. This is so that the method can examine the expression to do interesting things like find out the names of method calls in the body of the delegate instance. Problem is that you lose some of the int...

What is the best way to ReadLine by Expression Tree?

If I want to get a user input from Console to my Expression Tree. What is the best way to do it? and how to make variable 'name' duck typing? Here are my code. using System; using System.Reflection; using System.Collections.Generic; using Microsoft.Linq; using Microsoft.Linq.Expressions; namespace ExpressionTree { class Program ...

Patterns or techniques for designing a flexible advanced search with Linq Expression Trees

I'm looking to add an "advanced search" capability to my ASP.NET/SQL Server 2005 application. Ideally, I'd like it to be table driven. For example, if my schema changes with the addition of a new column to a table that I want to search, I'd like to have the UI reflect the addition of the new column as a searchable field. I can envisio...

C# LINQ Where Predicate Type Arguments

I have an XElement with values for mock data. I have an expression to query the xml: Expression<Func<XElement, bool>> simpleXmlFunction = b => int.Parse(b.Element("FooId").Value) == 12; used in: var simpleXml = xml.Elements("Foo").Where(simpleXmlFunction).First(); The design time error is: The type arguments for method 'Sy...

how to create expression tree / lambda for a deep property from a string

Hi, Given a string: "Person.Address.Postcode" I want to be able to get/set this postcode property on an instance of Person. How can I do this? My idea was to split the string by "." and then iterate over the parts, looking for the property on the previous type, then build up an expression tree that would look something like (apologies f...

C# Expression Tree Parameter for Parent Navigation Property

How can I create a ParameterExpression for the parent side of a 1 to * Navigation Property? The following works for the child entity: var parameter = Expression.Parameter( typeof(T), // where T is the entity type GetParameterName()); // helper method to get alias Trying something similar on TParent produces a query originatin...

How do I combine two Member Expression Trees?

I'm trying to combine the following expressions into a single expression: item => item.sub, sub => sub.key to become item => item.sub.key. I need to do this so I can create an OrderBy method which takes the item selector separately to the key selector. This can be accomplished using one of the overloads on OrderBy and providing an ICompa...

C#, Linq2Sql: Is it possible to concatenate two queryables into one?

I have one queryable where I have used various Where and WhereBetween statements to narrow the collection down to a certain set. Now I need to add kind of a Where || WhereBetween. In other words, I can't just chain them together like I have up till now, cause that will work as an And. So, how can I do this? I see two possibilities: Cr...

How do I include an InvocationExpression that is only evaluated once in an expression tree for linq to sql?

I'm trying to manually combine expression trees to accomplish a level of modularity that seems to allude me using the standard linq operators. The code essentially creates an expression tree that uses one expression to decide which of two other expressions to call. One of the other expressions requires an extra parameter that is itself o...

C# Dynamic Trees for Genetic Programming

I have some public user defined classes with relations between their members and also several methods with specific and generic signatures. I would like to be able to store and manipulate custom control flow over these classes (plus CLR classes) using basic control statements like if/then/else, foreach, do/while, variable assignments et...

What is the best way to encapsulate Linq to SQL data access?

I've been trying to encapsulate the object mapping in a projects data repository. Perhaps EF will provide the level of abstraction required but for a range of reasons I am using Linq to SQL at the moment. The following code aims to return the users in the database as a list of ModUser objects, where ModUser is POCO that the repository ex...

How to create LINQ Expression Tree with anonymous type in it

I would like to generate the following select statement dynamically using expression trees: var v = from c in Countries where c.City == "London" select new {c.Name, c.Population}; I have worked out how to generate var v = from c in Countries where c.City == "London" select new {c.Name}; but I cannot ...

Expression trees for dummies?

I am the dummy in this scenario. I've tried to read on Google what these are but I just don't get it. Can someone give me a simple explanation of what they are and why they're useful? edit: I'm talking about the LINQ feature in .Net. ...

C# How to convert an Expression<Func<SomeType>> to an Expression<Func<OtherType>>

I have used C# expressions before based on lamdas, but I have no experience composing them by hand. Given an Expression<Func<SomeType, bool>> originalPredicate, I want to create an Expression<Func<OtherType, bool>> translatedPredicate. In this case SomeType and OtherType have the same fields, but they are not related (no inheritance and...

Retrieving Property name from lambda expression

Is there a better way to get the Property name when passed in via a lambda expression? Here is what i currently have. eg. GetSortingInfo<User>(u => u.UserId); It worked by casting it as a memberexpression only when the property was a string. because not all properties are strings i had to use object but then it would return a unary...

Compiling Linq to SQL queries from a non-trivial IQueryable

Is there a way to use the CompiledQuery.Compile method to compile the Expression associated with an IQueryable? Currently I have an IQueryable with a very large Expression tree behind it. The IQueryable was built up using several methods which each supply components. For example, two methods may return IQueryables which are then joined i...