none
expression generation problem RRS feed

  • Question

  • Hello ..
    I'm having trouble understanding something w/ Linq to Sql ..
    Here's a short example that shows my problem:

    These both compile, but only the top one works as expected:

     

            static public Expression <Func <Person , bool >> blah()

            {

                DateTime date = DateTime .Parse("10/10/2009" );

                string title = "master of technology" ;

                return (p => p.Titles

                    .Any(t => (date == DateTime .MinValue ||

                              (t.StartDate <= date &&

                              (!t.EndDate.HasValue || t.EndDate.Value >= date))) &&

                              t.TitleDescription == title));

            }

            static public Expression <Func <Person , bool >> blah2()

            {

     

                DateTime date = DateTime .Parse("10/10/2009" );

                string title = "master of technology" ;

               

               // Changing this to Expression<Func<Title, bool>> won't compile,

               //   as noted in http://social.msdn.microsoft.com/forums/en-US/linqprojectgeneral/thread/121ec4e8-ce40-49e0-b715-75a5bd0063dc/?prof=required

                Func <Title, bool > predicate =

                    t => (date == DateTime .MinValue ||

                          (t.StartDate <= date &&

                          (!t.EndDate.HasValue || t.EndDate.Value >= date))) &&

                          t.TitleDescription == title;

     

                return (p => p.Titles.Any(predicate);

            }

    Later on we do something like:

     

             var people = linqDataContext.People.Select(p => p);

             List <Expression <Func <Person , bool >>> predicates = new List <Expression <Func <Person , bool >>>();

             predicates.Add(blah());

             foreach (Expression <Func <Person , bool >> pred in predicates)

             {

                 people = people.Where(pred);

             }

             var peopleList = people.ToList();

    In the case of blah2(), everything works as expected, but with blah() we get "NotSupportedException: Unsupported overload used for query operator 'Any'."  How is a different version of "Any()" being used by blah2()?

    Any way we can get this to work without using System.Linq.Dynamic (I'd rather keep it type-safe .. otherwise we might as well just create the SQL query ourselves)?

    Thursday, October 29, 2009 4:55 PM

Answers

  • Hi cwa_,

    The reason to this problem is that the compiler would generate a subquery when it translates the clause in the keyword 'Any()', and LINQ to SQL can't deal with references to expressions within this subquery.

    Well, for you case, I think you have already given a workaround yourself. And if you really want to erase the error, you can use compiled queries to have a try.

    For compiled queries, you can refer to:

    http://msdn.microsoft.com/en-us/library/bb896297.aspx
    Best regards,
    Charlie Lee

    Please remember to mark the replies as answers if they help and unmark them if they provide no help.
    • Marked as answer by Zhipeng Lee Wednesday, November 4, 2009 9:28 AM
    Monday, November 2, 2009 8:50 AM