LINQ Expressions 


What are the advantages to LINQ Expressions?

 

They can be useful in creating compilers, business "language" for business users, and can be especially useful if writing a business rules engine.

 

using System;

using System.Collections.Generic;

using System.Linq;

using System.Linq.Expressions;

using System.Text;

namespace ConsoleApplication77

{

    class Program

    {

        static void Main(string[] args)

        {

            // Create a parameter for entry to the method

            ParameterExpression x = Expression.Parameter(typeof(int), "x");

            // Create a couple of values to use.

            Expression fifteen = Expression.Constant(15);

            Expression twenty = Expression.Constant(20);

            // create the addition of x + 15

            Expression addition = Expression.Add(x, fifteen);

            // compare that expression against twenty {(x + 15) < 20}

            Expression lessthanten = Expression.LessThan(addition, twenty);

            // Generate the generic expression.

            Expression<Func<int, bool>> exp =

                Expression.Lambda<Func<int, bool>>(lessthanten, x);

            // Compile the expression.

            Func<int, bool> compiledExpression = exp.Compile();

            // Run the function.

            Console.WriteLine(compiledExpression(15));

            Console.WriteLine(compiledExpression(2));

        }

    }

}

 

 


 

using System;

using System.Collections.Generic;

using System.Linq;

using System.Text;

using System.Linq.Expressions;

using System.Diagnostics;

using System.Reflection;

namespace ConsoleApplication76

{

    class Program

    {

        static void Main(string[] args)

        {

            string ret = null;

            Foo foo = new Foo();

            Stopwatch sw = null;

            PropertyInfo reflectionAccessor = GetReflectionAccessor<Foo>();

            Func<Foo, string> expressionsAccessor = GetExpressionAccessor<Foo, string>();

            sw = Stopwatch.StartNew();

            for (int i = 0; i < 1000000; i++)

                ret = (string)reflectionAccessor.GetValue(foo, null);

            Console.WriteLine(sw.ElapsedMilliseconds);

            sw = Stopwatch.StartNew();

            for (int i = 0; i < 1000000; i++)

                ret = expressionsAccessor(foo);

            Console.WriteLine(sw.ElapsedMilliseconds);

            sw = Stopwatch.StartNew();

            for (int i = 0; i < 1000000; i++)

                ret = (string)GetProperty(foo, "Value");

            Console.WriteLine(sw.ElapsedMilliseconds);

        }

        public class Foo

        {

            public Foo()

            {

                Value = "Hello World!";

            }

            public string Value { get; set; }

        }

        static PropertyInfo GetReflectionAccessor<T>()

        {

            Type t = typeof(T);

            return t.GetProperty("Value");

        }

        static Func<T, TResult> GetExpressionAccessor<T, TResult>()

        {

            ParameterExpression ret = Expression.Parameter(typeof(T), "obj");

            Expression<Func<T, TResult>> exp = Expression.Lambda<Func<T, TResult>>(Expression.Property(ret, "Value"), ret);

            Func<T, TResult> func = exp.Compile();

            return func;

        }

        static T Add<T>(T arg1, T arg2)

        {

            ParameterExpression lhs = Expression.Parameter(typeof(T), "lhs");

            ParameterExpression rhs = Expression.Parameter(typeof(T), "rhs");

            Expression<Func<T, T, T>> exp = Expression.Lambda<Func<T, T, T>>(Expression.Add(lhs, rhs), lhs, rhs);

            Func<T, T, T> func = exp.Compile();

            return func(arg1, arg2);

        }

        public static Dictionary<string, Delegate> _delegates = new Dictionary<string, Delegate>();

        public static object GetProperty(object value, string property)

        {

            Delegate result = null;

            if (!_delegates.TryGetValue(property, out result))

            {

                ParameterExpression ret = Expression.Parameter(value.GetType(), "obj");

                LambdaExpression exp = Expression.Lambda(Expression.Property(ret, "Value"), ret);

                result = exp.Compile();

                _delegates.Add(property, result);

            }

            return result.DynamicInvoke(value);

        }

        static T Multiply<T>(T arg1, T arg2)

        {

            ParameterExpression lhs = Expression.Parameter(typeof(T), "lhs");

            ParameterExpression rhs = Expression.Parameter(typeof(T), "rhs");

            Expression<Func<T, T, T>> exp = Expression.Lambda<Func<T, T, T>>(Expression.Multiply(lhs, rhs), lhs, rhs);

            Func<T, T, T> func = exp.Compile();

            return func(arg1, arg2);

        }

        static T Square<T>(T x)

        {

            ParameterExpression lhs = Expression.Parameter(typeof(T), "lhs");

            ParameterExpression rhs = Expression.Parameter(typeof(T), "rhs");

            Expression<Func<T, T, T>> exp = Expression.Lambda<Func<T, T, T>>(Expression.Add(lhs, rhs), lhs, rhs);

            Func<T, T, T> func = exp.Compile();

            return func(x, x);

        }

    }

}

 


 

'Nuff said.