Complement for System.Linq.Expressions
C#
Latest commit e3372fb Aug 18, 2016 @jbevain committed on GitHub Merge pull request #14 from radical/fix-pcl-build
Mono.Linq.Expressions.csproj: Do not unset `TargetFrameworkProfile` ..
Failed to load latest commit information.
Mono.Linq.Expressions Fix CSharpWriter.VisitNew() when invoking struct default constructor Dec 15, 2015
Properties
Test Fix CSharpWriter.VisitNew() when invoking struct default constructor Dec 15, 2015
dbg forgot to commit dbg May 3, 2010
tools Update constraint patcher Jan 13, 2012
.gitignore Update .gitignore Jan 11, 2012
Mono.Linq.Expressions.csproj Mono.Linq.Expressions.csproj: Do not unset `TargetFrameworkProfile` .. Aug 18, 2016
Mono.Linq.Expressions.nunit fix unit tests May 12, 2010
Mono.Linq.Expressions.nuspec Bump version to 1.2 Jan 11, 2012
Mono.Linq.Expressions.sln forgot to commit dbg May 3, 2010
Mono.Linq.Expressions.targets Fix the Release configuration build on !Windows Jul 11, 2016
README.md Fixup readme. Jan 11, 2012
mono.snk initial commit May 3, 2010
nodes.txt add support for more nodes Jun 23, 2010

README.md

Mono.Linq.Expressions

Mono.Linq.Expressions is an helper library to complement the System.Linq.Expressions namespace.

It works on both Mono >= 2.8 and .net >= 4.0.

API


public static class CSharp {
    public string ToCSharpCode (Expression) {}
}

Returns a string containing the C# representation of the expression.


public static class FluentExtensions {}

Provides extension methods to ease the creation of expression trees. For instance, instead of writing:

var field = Expression.Field (
    Expression.Convert (parameter, typeof (string)),
    "Length");

You can write:

var field = parameter.Convert (typeof (string)).Field ("Length");

public abstract class CustomExpression {
    public abstract Expression Accept (CustomExpressionVisitor visitor) {}
}

Base class for custom expressions. Accept takes a custom visitor which extends the built-in ExpressionVisitor with support for custom expressions.


public static class CombineExtensions {
    public static Expression<T> Combine<T> (
        this Expression<T> self,
        Func<Expression, Expression> combinator) where T : Delegate {}

    public static Expression<T> Combine<T> (
        this Expression<T> self,
        Expression<T> expression,
        Func<Expression, Expression, Expression> combinator) where T : Delegate {}
}

Helper to combine a fully created lambda with another into a brand new lambda. This is done by rewriting and inlining the bodies in the resulting lamba. With this, combining two predicates with a And expression or negating an expression can be simply written:

public static Expression<Func<T, bool>> AndAlso<T> (
    this Expression<Func<T, bool>> self,
    Expression<Func<T, bool>> expression)
{
    return self.Combine (expression, Expression.AndAlso);
}

public static Expression<Func<T, bool>> Not<T> (
    this Expression<Func<T, bool>> self)
{
    return self.Combine (Expression.Not);
}

public abstract class ExpressionWriter {}

Provides a base class for pretty print specific language, such as CSharpWriter used by CSharp.ToCSharpCode().


public class DoWhileExpression : CustomExpression {}

A do {} while (condition); statement.


public class ForEachExpression : CustomExpression {}

A foreach (var item in iterable) {} statement.


public class ForExpression : CustomExpression {}

A for (initializer; condition; increment) {} statement.


public class UsingExpression : CustomExpression {}

A using (disposable) {} statement.


public class WhileExpression : CustomExpression {}

A while (condition) {} statement.


public static class PredicateBuilder {

    public static Expression<Func<T, bool>> OrElse<T> (
        this Expression<Func<T, bool>> self,
        Expression<Func<T, bool>> expression) {}

    public static Expression<Func<T, bool>> AndAlso<T> (
        this Expression<Func<T, bool>> self,
        Expression<Func<T, bool>> expression) {}

    public static Expression<Func<T, bool>> Not<T> (
        this Expression<Func<T, bool>> self) {}
}

Provides a way to combine lambda predicates using boolean operators. Expressions are rewritten to keep the predicates simple and understandable by LINQ providers. For instance:

Expression<Func<User, bool>> isOver18 = u => u.Age > 18;
Expression<Func<User, bool>> isFemale = u => u.Gender == Gender.Female;

Expression<Func<User, bool>> femalesOver18 = isOver18.AndAlso (isFemale);

// >> femalesOver18.ToString ()
// u => u.Age > 18 && u.Gender == Gender.Female