locked
IObjectSet Include Extension Method Errors with CompiledQuery RRS feed

  • Question

  • I am using EF CTP4 and in my Custom ObjectContext class I have my entity collections exposed as IObjectSet so they can be unit-tested. I have run into a problem when I use this ObjectContext in a compiled query and call the "Include" extension method (From Julie Lerman's blog http://thedatafarm.com/blog/data-access/agile-entity-framework-4-repository-part-5-iobjectset/ ):

     

     
    
    
    
    public IQueryable<MyPocoObject> RunQuery(MyCustomContext context, int theId)
     {
      var query = CompiledQuery.Compile<MyCustomContext, int, IQueryable<MyPocoObject>>(
        (ctx, id) => ctx.MyPocoObjects.Include("IncludedPocoObject").Where(n => n.IncludedPocoObject.id == id));
    
      return query(context, theId);
     }


    LINQ to Entities does not recognize the method 'System.Linq.IQueryable1[MyPocoObject] Include[MyIncludedPocoObject](System.Linq.IQueryable 1[MyPocoObject], System.String)' method, and this method cannot be translated into a store expression.

    If I use this same query on ObjectSet collections rather than IObjectSet it works fine. If I simply run this query without precompiling it works fine. What am I missing here?


    • Moved by Rowan Miller Wednesday, August 11, 2010 6:16 PM Moving from pre-release forum (From:ADO.NET Entity Framework and LINQ to Entities (Pre-Release))
    Wednesday, August 11, 2010 6:34 AM

Answers

  • Hello,

    Thanks for your thread!

    Based on the link you provided, I noticed there is an extension method for IQueryable<TSource>. Please try to add this method and build it again. By default, the Include method is for ObjectQuery.

    Best regards


    Please remember to mark the replies as answers if they help and unmark them if they provide no help.
    If you have any feedback, please tell us.
    Welcome to the All-In-One Code Framework!
    Monday, August 16, 2010 8:01 AM

All replies

  • Hello,

    Thanks for your thread!

    Based on the link you provided, I noticed there is an extension method for IQueryable<TSource>. Please try to add this method and build it again. By default, the Include method is for ObjectQuery.

    Best regards


    Please remember to mark the replies as answers if they help and unmark them if they provide no help.
    If you have any feedback, please tell us.
    Welcome to the All-In-One Code Framework!
    Monday, August 16, 2010 8:01 AM
  • You can remove the error by explicitly casting the ObjectSet<T> to an ObjectQuery<T> in your compiled query.

     

    public IQueryable<MyPocoObject> RunQuery(MyCustomContext context, int theId)
     {
     var query = CompiledQuery.Compile<MyCustomContext, int, IQueryable<MyPocoObject>>(
     (ctx, id) => ((ObjectQuery<MyPocoObject>)ctx.MyPocoObjects).Include("IncludedPocoObject").Where(n => n.IncludedPocoObject.id == id));
    
     return query(context, theId);
     }

    The error is due to the way extension methods are resolved. Upcasting to the base ObjectQuery<T> forces the compiler to choose the inherited method instead of the extension.

    Also, be sure you're storing your complied queries in a static variable so that they will be reused.

    • Proposed as answer by jowenece Wednesday, October 6, 2010 5:21 PM
    • Edited by jowenece Wednesday, October 6, 2010 8:47 PM typo
    Wednesday, October 6, 2010 4:50 PM