none
Dynamic query and compile RRS feed

  • Question

  • I understand the syntax and usage of compiledquery. However, I have some dynamic query that check for user selection and construct the query @ run time.  For example,

    if (dateconstraint)

    {

    commonquery = from q in commonquery

                                      where q.DateCreated >= minDateRange

       select q;

    }

    My question is how to compile this dynamic query as the CompiledQuery.Compiled cannot have any statements other than query statement.

    Thanks

    Friday, June 7, 2013 4:58 PM

Answers

  • Hi nativecpp,

    If what you want is to create a chained compiled query, I'm afraid it is not supported. LINQ to Entities statement cannot contains another Invoke method. It is something like LINQ to SQL:

    http://stackoverflow.com/questions/1544006/can-multiple-compiled-linq-queries-be-chained-together

    If there are multiple compiled queries, I think you may try something like:

    var cq1 = CompiledQuery.Compile<Entities1, String, IQueryable<Address>>(
        (ctx, start) => from a in ctx.Addresses
                        where a.Code.StartsWith(start)
                        select a);
    var result1 = cq1(context, "1");
    
    var cq2 = CompiledQuery.Compile<Entities1, String, IQueryable<Address>>(
        (ctx, end) => from a in result1
                          where a.Code.EndsWith(end)
                          select a);
    var result2 = cq2(context, "2");
    

    If it isn't what you want, please feel free to let us know.

    Best regards,


    Chester Hong
    MSDN Community Support | Feedback to us
    Develop and promote your apps in Windows Store
    Please remember to mark the replies as answers if they help and unmark them if they provide no help.

    Wednesday, June 12, 2013 5:29 AM
    Moderator

All replies

  • Forgot to add that I actually have a wrapper function that returns IQueryable of some type  for the construction of my dynamic query. 
    Friday, June 7, 2013 5:02 PM
  • Hi nativecpp,

    If what you want is to create a chained compiled query, I'm afraid it is not supported. LINQ to Entities statement cannot contains another Invoke method. It is something like LINQ to SQL:

    http://stackoverflow.com/questions/1544006/can-multiple-compiled-linq-queries-be-chained-together

    If there are multiple compiled queries, I think you may try something like:

    var cq1 = CompiledQuery.Compile<Entities1, String, IQueryable<Address>>(
        (ctx, start) => from a in ctx.Addresses
                        where a.Code.StartsWith(start)
                        select a);
    var result1 = cq1(context, "1");
    
    var cq2 = CompiledQuery.Compile<Entities1, String, IQueryable<Address>>(
        (ctx, end) => from a in result1
                          where a.Code.EndsWith(end)
                          select a);
    var result2 = cq2(context, "2");
    

    If it isn't what you want, please feel free to let us know.

    Best regards,


    Chester Hong
    MSDN Community Support | Feedback to us
    Develop and promote your apps in Windows Store
    Please remember to mark the replies as answers if they help and unmark them if they provide no help.

    Wednesday, June 12, 2013 5:29 AM
    Moderator