none
Compiled Query Error at "Where" Expression RRS feed

  • Question

  • Hello friends !

    I started to working with Compiled Query.I use Linq to Sql Data Context and compiled query mechanism (with AsParallel()).My code is below.

     

          var context = new AdventureWorksDataContext();

     

                  var query = CompiledQuery.Compile<AdventureWorksDataContext, IEnumerable<Paycheck>>(

                      (cnx) =>

                         (from row in cnx.Paychecks.AsParallel()

                          where row.PaycheckId > 50000

                          select row)

                    );

                  var rowCount = query(context).Count();

    And I was getting error : InvalidOperationExcetion -> message : The method 'Where' is not mapped as a stored procedure or user-defined function.

    When I removed "AsParallel()" expression,I didn't get error.I want to use both of Parallel and Compiled Linq.

    Happy Coding Days...

     

    Monday, February 14, 2011 3:09 PM

Answers

  • Hi C Sharp Coder;

    The PLinq extension is an extension that works on in memory collections so applying it to a Linq to SQL or Linq to Entity will not work. This is because Linq to SQL or Linq to Entity translate the linq query into native SQL and native SQL does not have a AsParallel equivalent therefore it can not be converted.

    You can most likely download the data from the server and then run the AsParallel method on the local in memory objects but that will most likely not buy you much.

    Fernando

    • Marked as answer by C Sharp Coder Tuesday, February 22, 2011 6:22 PM
    Sunday, February 20, 2011 4:20 PM

All replies

  • Hello C Sharp Coder,

     

    Welcome to the LINQ to SQL Forum and thanks for posting here.

    According to your description, I think could you please try to write the lamda expression instead of linq query?

    Like this example:

    var q2 = orders.AsParallel()
           .Where(o => o.OrderDate < DateTime.Parse("07/04/1997"))
           .Select(o => o)
           .OrderBy(o => o.CustomerID) // Preserve original ordering for Take operation.
           .Take(20)


    I hope this will help you.

     

    have a nice day,


    Jackie Sun [MSFT]
    MSDN Community Support | Feedback to us
    Get or Request Code Sample from Microsoft
    Please remember to mark the replies as answers if they help and unmark them if they provide no help.

    Wednesday, February 16, 2011 7:38 AM
    Moderator
  • Hello C Sharp Coder,

     

    Welcome to the LINQ to SQL Forum and thanks for posting here.

    According to your description, I think could you please try to write the lamda expression instead of linq query?

    Like this example:

    var q2 = orders.AsParallel()
           .Where(o => o.OrderDate < DateTime.Parse("07/04/1997"))
           .Select(o => o)
           .OrderBy(o => o.CustomerID) // Preserve original ordering for Take operation.
           .Take(20)


    I hope this will help you.

     

    have a nice day,


    Jackie Sun [MSFT]
    MSDN Community Support | Feedback to us
    Get or Request Code Sample from Microsoft
    Please remember to mark the replies as answers if they help and unmark them if they provide no help.

    Very thanks for your's answer.But now also I got : "LINQ to Entities does not recognize the method 'System.Linq.ParallelQuery`1[.Paycheck] Where[Paycheck](System.Linq.ParallelQuery`1[Paycheck], System.Func`2[.Paycheck,System.Boolean])' method, and this method cannot be translated into a store expression."}

     

    My code;

                  SampleDBEntities context = new SampleDBEntities();

     

                    var delege =CompiledQuery.Compile<SampleDBEntities,IEnumerable<Paycheck>>(

                        (cx)=>    cx.Paychecks.AsParallel().Where(row => row.PaycheckId > 500000)

                        );

    delege(context).Count().ToString();

    Saturday, February 19, 2011 12:58 PM
  • Hi C Sharp Coder;

    The PLinq extension is an extension that works on in memory collections so applying it to a Linq to SQL or Linq to Entity will not work. This is because Linq to SQL or Linq to Entity translate the linq query into native SQL and native SQL does not have a AsParallel equivalent therefore it can not be converted.

    You can most likely download the data from the server and then run the AsParallel method on the local in memory objects but that will most likely not buy you much.

    Fernando

    • Marked as answer by C Sharp Coder Tuesday, February 22, 2011 6:22 PM
    Sunday, February 20, 2011 4:20 PM
  • Hi C Sharp Coder;

    The PLinq extension is an extension that works on in memory collections so applying it to a Linq to SQL or Linq to Entity will not work. This is because Linq to SQL or Linq to Entity translate the linq query into native SQL and native SQL does not have a AsParallel equivalent therefore it can not be converted.

    You can most likely download the data from the server and then run the AsParallel method on the local in memory objects but that will most likely not buy you much.

    Fernando

    Thanks.I understand it.
    Tuesday, February 22, 2011 6:22 PM