none
Convert IQueryable(Of T) to Expression(Of Func(Of T, Boolean)) RRS feed

  • Question

  • How do I convert an IQueryable(Of T) to an Expression(Of Func(Of T, Boolean))?


    I've used DynamicQuery to turn a string into an expression, but that doesn't really apply here (I show it to give an idea what I'm looking for):
    DynamicExpression.ParseLambda(Of T, Boolean)(tempFieldName & " <= @0", laterDate)

    I can give more context if anyone is interested, but I think that basically sums up my question.

    Thanks!
    -Jerome
    Thursday, January 28, 2010 5:42 AM

All replies

  • Take a look at the IQueryable.Expression property. It contains the expression tree for IQueryable.
    Devart Team
    http://www.devart.com/dotconnect
    ADO.NET data providers for Oracle, MySQL, PostgreSQL, SQLite with Entity Framework and LINQ to SQL support
    Friday, January 29, 2010 12:19 PM
  • Thanks for your reply.  I did look at that property.  It is a MethodCallExpression.  I cannot simply cast it into an Expression(Of Func(Of T, Boolean)).  So I suppose that I would better specify my original question with the title:

    How do I convert IQueryable(Of T).Expression {MethodCallExpression} into Expression(Of Func(Of T, Boolean)).

    What needs to be done to that MethodCallExpression to massage it into the correct form?

    Thanks!
    -Jerome
    Friday, January 29, 2010 3:07 PM
  • The Tree Structure is something like: 
    MethodCallExpression (MCE) 
    UnaryExpression (UE) 
    ConstantExpression (CE) 

    (MCE1) 
    (MCE1):(MCE2)(UE1) 
    (MCE2):(MCE3)(UE2) 
    (MCE3):(MCE4)(UE3) 
    (MCE4):(CE1)
    • Edited by Jeromeyers Friday, January 29, 2010 3:22 PM formatting
    Friday, January 29, 2010 3:21 PM
  • Is this question dead in the water?

    Thanks,
    -Jerome
    Monday, February 1, 2010 5:19 AM
  • You are comparing the T class in IQueryable (here T is an entity type) and the T class in ParseLambda (here it is a parameter type). Are you sure this is correct?
    Maybe this compiled query will help you.  

    public static Func HasDate = Devart.Data.Linq.CompiledQuery.Compile((TESTDataContext.TESTDataContext ctx, DateTime date) => (ctx.EMPs.Any(emp => emp.HIREDATE <= date))); 
    where T is DateTime. Not so abstract but works. This is how to call it:
    
    TESTDataContext.TESTDataContext db = new TESTDataContext.TESTDataContext();
    bool dsd = HasDate(db, DateTime.Now);
    

    Devart Team
    http://www.devart.com/dotconnect
    ADO.NET data providers for Oracle, MySQL, PostgreSQL, SQLite with Entity Framework and LINQ to SQL support
    Tuesday, February 2, 2010 2:35 PM