[ACCEPTED]-Dynamic linq Building Expression-dynamic-linq

Accepted answer
Score: 11

You can use the method described here.

You would 4 need to cast the result of the method to 3 Expression<Func<T,bool>>. T being your type.

I will provide an complete 2 example when i get home.

Edit:

using System;
using System.Collections.Generic;
using System.Linq;
using System.Linq.Expressions;
using System.Collections;
using System.Reflection;

namespace ExpressionPredicateBuilder
{
    public enum OperatorComparer
    {
        Contains,
        StartsWith,
        EndsWith,
        Equals = ExpressionType.Equal,
        GreaterThan = ExpressionType.GreaterThan,
        GreaterThanOrEqual = ExpressionType.GreaterThanOrEqual,
        LessThan = ExpressionType.LessThan,
        LessThanOrEqual = ExpressionType.LessThan,
        NotEqual = ExpressionType.NotEqual        
    }

public class ExpressionBuilder
{
    public static Expression<Func<T,bool>> BuildPredicate<T>(object value, OperatorComparer comparer, params string[] properties)
    {
        var parameterExpression = Expression.Parameter(typeof(T), typeof(T).Name);
        return (Expression<Func<T, bool>>)BuildNavigationExpression(parameterExpression, comparer, value, properties);
    }

    private static Expression BuildNavigationExpression(Expression parameter, OperatorComparer comparer, object value, params string[] properties)
    {
        Expression resultExpression = null;
        Expression childParameter, predicate;
        Type childType = null;

        if (properties.Count() > 1)
        {
            //build path
            parameter = Expression.Property(parameter, properties[0]);
            var isCollection = typeof(IEnumerable).IsAssignableFrom(parameter.Type);
            //if it´s a collection we later need to use the predicate in the methodexpressioncall
            if (isCollection)
            {
                childType = parameter.Type.GetGenericArguments()[0];
                childParameter = Expression.Parameter(childType, childType.Name);
            }
            else
            {
                childParameter = parameter;
            }
            //skip current property and get navigation property expression recursivly
            var innerProperties = properties.Skip(1).ToArray();
            predicate = BuildNavigationExpression(childParameter, comparer, value, innerProperties);
            if (isCollection)
            {
                //build subquery
                resultExpression = BuildSubQuery(parameter, childType, predicate);
            }
            else
            {
                resultExpression = predicate;
            }
        }
        else
        {
            //build final predicate
            resultExpression = BuildCondition(parameter, properties[0], comparer, value);
        }
        return resultExpression;
    }

    private static Expression BuildSubQuery(Expression parameter, Type childType, Expression predicate)
    {
        var anyMethod = typeof(Enumerable).GetMethods().Single(m => m.Name == "Any" && m.GetParameters().Length == 2);
        anyMethod = anyMethod.MakeGenericMethod(childType);
        predicate = Expression.Call(anyMethod, parameter, predicate);
        return MakeLambda(parameter, predicate);
    }

    private static Expression BuildCondition(Expression parameter, string property, OperatorComparer comparer, object value)
    {
        var childProperty = parameter.Type.GetProperty(property);
        var left = Expression.Property(parameter, childProperty);
        var right = Expression.Constant(value);
        var predicate = BuildComparsion(left, comparer, right);
        return MakeLambda(parameter, predicate);
    }

    private static Expression BuildComparsion(Expression left, OperatorComparer comparer, Expression right)
    {
        var mask = new List<OperatorComparer>{
            OperatorComparer.Contains,
            OperatorComparer.StartsWith,
            OperatorComparer.EndsWith
        };
        if(mask.Contains(comparer) && left.Type != typeof(string))
        {
            comparer = OperatorComparer.Equals;
        }
        if(!mask.Contains(comparer))
        {
            return Expression.MakeBinary((ExpressionType)comparer, left, Expression.Convert(right,left.Type));
        }
        return BuildStringCondition(left, comparer, right);            
    }

    private static Expression BuildStringCondition(Expression left, OperatorComparer comparer, Expression right)
    {
        var compareMethod = typeof(string).GetMethods().Single(m => m.Name.Equals(Enum.GetName(typeof(OperatorComparer), comparer)) && m.GetParameters().Count() == 1);
        //we assume ignoreCase, so call ToLower on paramter and memberexpression
        var toLowerMethod = typeof(string).GetMethods().Single(m => m.Name.Equals("ToLower") && m.GetParameters().Count() == 0);
        left = Expression.Call(left, toLowerMethod);
        right = Expression.Call(right, toLowerMethod);
        return Expression.Call(left, compareMethod, right);
    } 

    private static Expression MakeLambda(Expression parameter, Expression predicate)
    {
        var resultParameterVisitor = new ParameterVisitor();
        resultParameterVisitor.Visit(parameter);
        var resultParameter = resultParameterVisitor.Parameter;
        return Expression.Lambda(predicate, (ParameterExpression)resultParameter);
    }

    private class ParameterVisitor : ExpressionVisitor
    {
        public Expression Parameter
        {
            get;
            private set;
        }
        protected override Expression VisitParameter(ParameterExpression node)
        {
            Parameter = node;
            return node;
        }
    }
}

}

This can 1 be used like

var predicate = ExpressionBuilder.BuildPredicate<Books>("Heading",OperatorComparer.Equals,"Page","Heading");
query = query.Where(predicate);
Score: 3

Based on your description I'm not sure that 19 you need Expression. Creating an Expression with a complex object 18 model is quite difficult. Do you really 17 need to create dynamic expression or you 16 simply need to create a dynamic query? If 15 the object model is fixed then you don't 14 need Expression.

I suggest first of all to clean your 13 object model:

  • Rename the Books class to Book (this class represents a Book not a list of books)
  • Rename property Page to Pages (this property returns a list of pages)

Now you can write a dynamic 12 where using just LINQ and one or more helper 11 functions one for each property that you 10 need to search. For example to search for 9 Heading you can write:

        private static bool SearchByHeading(Book b, string heading)
        {
            if (string.IsNullOrEmpty(heading))
                return true;
            else
                return b.Pages.Any(p => p.Heading == heading);
        }

Here you can also see why 8 your previous code didn't work. The expression 7 to search for a given Heading is book.Pages.Any(p => p.Heading == x) and not book.Pages.Heading == x.

In any 6 case given one or more functions like this 5 you can rewrite your code with something 4 like:

using System.Collections.Generic;
using System.Linq;

namespace XMLStorageAndFilter
{
    public class Book
    {
        public Book()
        {
            Pages = new List<Page>();
        }
        public string Title { get; set; }
        public Author Author { get; set; }
        public List<Page> Pages { get; set; }
    }
    public class Author
    {
        public string FirstName { get; set; }
    }
    public class Page
    {
        public string Heading { get; set; }
    }

    public class Program2
    {
        static void Main()
        {
            Page page = new Page();
            page.Heading = "Heading1";
            Book bok = new Book();
            bok.Title = "Title1";
            bok.Author = new Author() { FirstName = "FirstName1" };
            bok.Pages.Add(page);
            List<Book> testList = new List<Book>();
            testList.Add(bok);

            var searchResult = Search(testList, 
                title: "Title1",
                author: "FirstName1",
                heading: "Heading1");
        }

        private static IEnumerable<Book> Search(IEnumerable<Book> books, string author = null, string title = null, string heading = null)
        {
            return books
                .Where((b) => SearchByAuthor(b, author))
                .Where((b) => SearchByHeading(b, heading))
                .Where((b) => SearchByTitle(b, title))
                .ToList();
        }

        private static bool SearchByAuthor(Book b, string author)
        {
            if (string.IsNullOrEmpty(author))
                return true;
            else
                return b.Author.FirstName == author;
        }

        private static bool SearchByTitle(Book b, string title)
        {
            if (string.IsNullOrEmpty(title))
                return true;
            else
                return b.Title == title;
        }

        private static bool SearchByHeading(Book b, string heading)
        {
            if (string.IsNullOrEmpty(heading))
                return true;
            else
                return b.Pages.Any(p => p.Heading == heading);
        }
    }
}

I have skipped search values when null 3 or empty, just an example. This code has 2 also the advantage that is verified at compile 1 time.

Score: 1

Update

The way to query a collection has been answered 12 in the following:

Building a dynamic expression tree to filter on a collection property

Original Response

I believe Davide Lcardi 11 is correct with his statement:

Heading is book.Pages.Any(p => p.Heading == x) and not book.Pages.Heading == x.

if you want 10 to query the list you need to use Any() method 9 to do so. I could not get it exactly right 8 but it should look something like the following 7 using Expression.Call:

                ParameterExpression pe41 = Expression.Parameter(typeof (Page), "pg");
                Expression left41 = Expression.Property(pe41, "Heading");
                Expression right41 = Expression.Constant("Heading");
                Expression e41 = Expression.Equal(left41, right41);

                var methodCall = Expression.Call( Expression.Property(pe11, "Pages"), "Any", new Type[] {typeof(Page), typeof(Boolean)}, e41 );

I am getting this 6 error: No method 'Any' exists on type 'System.Collections.Generic.List`1[SO.Page]'. SO is my NameSpace where class 5 Page exists.

I think I am not sending the 4 correct Type or the whole call may be incorrect. I 3 think this is the correct direction thought 2 to help you find your solution.

Here are 1 some examples I was looking at:

http://msdn.microsoft.com/en-us/library/bb349020(v=vs.110).aspx

http://msdn.microsoft.com/en-us/library/dd402755(v=vs.110).aspx

http://community.bartdesmet.net/blogs/bart/archive/2009/08/10/expression-trees-take-two-introducing-system-linq-expressions-v4-0.aspx

http://blogs.msdn.com/b/csharpfaq/archive/2009/09/14/generating-dynamic-methods-with-expression-trees-in-visual-studio-2010.aspx

Score: 0

On the whole, it is not bad to consider 1 DynamicLinq when you are dealing with dynamic matters

More Related questions