Project Description
Library for working with C#/F# expressions (System.Linq.Expressions, Microsoft.FSharp.Quotations). Building expressions, converting C# expressions to F# expressions, active patterns for C# expressions, expression evaluation, ect.

I'm updating this project by need for my personal use. So, it has quite limited functional, but can be easily extended however. If you missing some functions, please create discussion or join and submit some code.

Building expressions

Sometimes it’s impossible to write down LINQ expression statically and one has to create it dynamically in runtime. .net provides some method to achieve it. First of all, it’s possible to assemble expression by hand, using System.Linq.Expressions.Expression class. But such approach is error-prone and yields very poor readability. Second, you can use any LINQ-compiler which translates string literals to LINQ expressions. Such compiler can be found on codeplex (for example: http://linqcompiler.codeplex.com/) it also included in C# code samples (http://code.msdn.microsoft.com/cs2008samples, see DynamicQuery) and .net web controls library (see LinqDataSource). Working with LINQ expression as with string is much easier, then assembling expression from Expression classes but it still imperfect – you lose all benefits of static type checking, refactoring, and syntax checking. LExpress provides an alternative way to compose expressions which allows you assemble expressions from static and dynamic parts, utilizing full power of C# programming language.

Lets consider a simple example. Suppose you have to write a filter component, which statically parameterized with entity type and field type, but field name itself passed as string parameter. With LExpress such component can be implemented like following:

public class MyFilter<TEntity, TField>
{
    public string FieldName;
    public TField FieldValue;

    public IQueryable<TEntity> Apply(IQueryable<TEntity> source)
    {
        var predicate = Transformers.Transform<Func<TEntity, bool>>(
            entity => entity.PropertyOrField<TField>(FieldName).Equal(FieldValue));

        return source.Where(predicate);
    }
}


We relay on extension methods here (PropertyOrField, Equal) to assemble expression, then use Transform method to replace extension method calls with corresponding expressions. After that expression can be used in the LINQ query.

Further we can use any information known at compile-time. Suppose we know, than field will be of String type. We can write queries like following:

    public IQueryable<TEntity> Apply(IQueryable<TEntity> source)
    {
        var predicate = Transformers.Transform<Func<TEntity, bool>>(
            entity => entity.PropertyOrField<string>(FieldName).StartsWith(FieldValue));

        return source.Where(predicate);
    }

Function PropertyOrField will be translated to property-access expression and StartsWith will be passed to LINQ translator as is.

Last edited Aug 27, 2010 at 3:12 PM by schernichkin, version 5