none
Precompiling does not work RRS feed

  • Question

  • This is running fine:


    Expression<Func<T, bool>> ex = e => e.Id == 0;
    GetTable<T>().SingleOrDefault(ex);


    This is running fine, too:


    var query = CompiledQuery.Compile((DataContext dc) => dc.GetTable<T>().SingleOrDefault(e => e.Id == 0));
    query(DataContext);


    But this is giving an error:


    Expression<Func<T, bool>> ex = e => e.Id == 0;
    var query2 = CompiledQuery.Compile((DataContext dc) => dc.GetTable<T>().SingleOrDefault(ex));
    query2(DataContext);


    It says: "Unsupported overload used for query operator 'SingleOrDefault'."

    Why is that?

    Wednesday, October 14, 2009 2:28 PM

Answers

  • Hi Sandro,

    You can find the root cause of this issue in this thread: http://social.msdn.microsoft.com/forums/en-US/linqprojectgeneral/thread/0371bf94-3518-462e-8d7e-bddcb6b4f09d/.

     

    Quote from Matt Warren (LINQ geek):

    “When you first built the query the expression called your expression-tree building function and the query was stitched together without a reference to your function in it (just tree that it built).  When you made a compiled query, the entire query expression was captured as an expression tree including the call to your method.  However, LINQ to SQL is not actually executing the expression tree, it is translating it to SQL.  When it gets to the expression node that represents the call to your method it is expecting an inline lambda expression.”

     

    Have a nice day!

     

    Best Regards,
    Lingzhi Sun


    Please remember to mark the replies as answers if they help and unmark them if they provide no help.
    Welcome to the All-In-One Code Framework! If you have any feedback, please tell us.
    • Marked as answer by Yichun_Feng Wednesday, October 21, 2009 1:03 AM
    Tuesday, October 20, 2009 8:43 AM
    Moderator