Home
Help
Register
Log in

Search

 
   Active Threads  

You are here: Home > LLBLGen Pro > LLBLGen Pro Runtime Framework> How to use Linq to LLBLGen Pro with a predicate builder?
 

Pages: 1
LLBLGen Pro Runtime Framework
How to use Linq to LLBLGen Pro with a predicate builder?
Page:1/1 

  Print all messages in this thread  
Poster Message
Otis
LLBLGen Pro Team



Location:
The Hague, The Netherlands
Joined on:
17-Aug-2003 18:00:36
Posted:
37186 posts
# Posted on: 03-Sep-2008 21:48:39.  
First, use our own predicate builder:
Code:

public static class PredicateBuilder
{
    public static System.Linq.Expressions.Expression<Func<T, bool>> Null<T>() { return null; }

    public static System.Linq.Expressions.Expression<Func<T, bool>> Or<T>(this System.Linq.Expressions.Expression<Func<T, bool>> expr1,
                                                        System.Linq.Expressions.Expression<Func<T, bool>> expr2)
    {
        if(expr1 == null)
        {
            return expr2;
        }
        ExpressionReplacer replacer = new ExpressionReplacer(CreateFromToReplaceSet(expr2.Parameters, expr1.Parameters), null, null, null, null);
        LambdaExpression rightExpression = (LambdaExpression)replacer.HandleExpression(expr2);
        
        return System.Linq.Expressions.Expression.Lambda<Func<T, bool>>
             (System.Linq.Expressions.Expression.OrElse(expr1.Body, rightExpression.Body), expr1.Parameters);
    }

    public static System.Linq.Expressions.Expression<Func<T, bool>> And<T>(this System.Linq.Expressions.Expression<Func<T, bool>> expr1,
                                                         System.Linq.Expressions.Expression<Func<T, bool>> expr2)
    {
        if(expr1 == null)
        {
            return expr2;
        }
        ExpressionReplacer replacer = new ExpressionReplacer(CreateFromToReplaceSet(expr2.Parameters, expr1.Parameters), null, null, null, null);
        LambdaExpression rightExpression = (LambdaExpression)replacer.HandleExpression(expr2);
        return System.Linq.Expressions.Expression.Lambda<Func<T, bool>>
             (System.Linq.Expressions.Expression.AndAlso(expr1.Body, rightExpression.Body), expr1.Parameters);
    }


    private static Dictionary<LinqExpression, LinqExpression> CreateFromToReplaceSet(IList<ParameterExpression> from,
                                        IList<ParameterExpression> to)
    {
        Dictionary<LinqExpression, LinqExpression> toReturn = new Dictionary<LinqExpression, LinqExpression>();
        for(int i = 0; i < from.Count; i++)
        {
            toReturn.Add(from[i], to[i]);
        }
        return toReturn;
    }
}


see this thread for more information and usage:
http://www.llblgen.com/tinyforum/Messages.aspx?ThreadID=14144

This way, you can build dynamic predicates using Linq which are merged into the expression tree and which are used for the final query on the DB.
Frans Bouma
LLBLGen Pro / ORM Profiler Lead Developer | Blog | Twitter
 
Top
Pages: 1  


Powered by HnD ©2002-2007 Solutions Design
HnD uses LLBLGen Pro

Version: 2.1.12172008 Final.