locked
LINQ on custom IObjectSet<T> implementation is not working for complex queries?! RRS feed

  • Question

  • I stumbled on this one today (Using VS2010 SP1 and EF 4.0 generated Selftracking entities):

    To build repositories with additional methods we had to substitute the ObjectSet instances returned from an ObjectContext by default with a custom implementation of IObjectSet<T>. These IRepository<T> wrap the frameworks ObjectSet and forward any call to the IObjectSets methods to the wrapped ObjectSet instance.

    Everything worked fine until we made a subquery with LINQ like shown in this example:

     

    var result = 
                        from t1 in model.Table1
                        where (
                            from t2 in model.Table2
                            where  t2.data == "test11"
                            select t2.data
                        ).Contains( t1.data)
                        select t1;
    

     

    This query was working perfectly with the ObjectSet but it throws now a NotSupportedException that says that only primitive types can be converted to constant expressions. As far as we could debug this with .Net Reflector the exception is thrown at

    ExpressionConverter.ConstantTranslator.TypedTranslate()
    {
     ...

     if( !flag2 )
     throw EntityUtil.NotSupported(Strings.ELinq_UnsupportedConstant(ExpressionConverter.DescribeClrType(linq.Type),Strings.ELinq_PrimitiveTypesSample)); 

     ...

    The funny thing is: if the subquery is build outside the linq statement it runs perfectly again:

     

    IQueryable<string> subquery =
                        from t2 in model.Table2
                        where t2.data == "test11"
                        select t2.data
                    ;
     
                    var result =
                        from t1 in model.Table1
                        where subquery.Contains(t1.data)
                        select t1;

     

     

    So the difference between working and not working is the usage of a custom IObjectSet<T> implementation within a LINQ statement! 

    Could this be a bug in LINQ's expression evaluation?

    I made a simple project to show the problem, which can be downloaded from: http://dl.dropbox.com/u/3724006/SubqueryTest.zip

    It contains all necessary code and a database project to create the examples database.

     

    We would be really happy if this problem could be solved, we really need these custom repositories.

     

    Best regards,

    Wolfgang Gross

     

     

     


    Friday, September 16, 2011 10:03 AM

Answers

  • We found a workaround or a possible Solution: If we don't delegate from our repository implementation to the wrapped ObjectSet but inherit from ObjectQuery, as the ObjectSet itself does, it works as expected.

    I am not sure if this can be really called a solution because its doesn't explain why LINQ ist not working fully with a IObjectSet instance, but at least for us and for now it could be sufficient. 

     

    Any more insights are very welcome!

     

    Best Regards,

    Wolfgang Gross

     

    • Marked as answer by Alan_chen Wednesday, September 28, 2011 3:03 AM
    Friday, September 16, 2011 1:01 PM
  • Hi Wolfgang,

    Welcome!

    I found a related post here: http://social.msdn.microsoft.com/Forums/en/adonetefx/thread/834e27aa-b2e7-4900-ae1c-e8e1b02de4ba, hope it helps.

    Have a nice day.


    Alan Chen[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.

    • Marked as answer by Alan_chen Wednesday, September 28, 2011 3:03 AM
    Sunday, September 18, 2011 9:07 AM

All replies

  • We found a workaround or a possible Solution: If we don't delegate from our repository implementation to the wrapped ObjectSet but inherit from ObjectQuery, as the ObjectSet itself does, it works as expected.

    I am not sure if this can be really called a solution because its doesn't explain why LINQ ist not working fully with a IObjectSet instance, but at least for us and for now it could be sufficient. 

     

    Any more insights are very welcome!

     

    Best Regards,

    Wolfgang Gross

     

    • Marked as answer by Alan_chen Wednesday, September 28, 2011 3:03 AM
    Friday, September 16, 2011 1:01 PM
  • Hi Wolfgang,

    Welcome!

    I found a related post here: http://social.msdn.microsoft.com/Forums/en/adonetefx/thread/834e27aa-b2e7-4900-ae1c-e8e1b02de4ba, hope it helps.

    Have a nice day.


    Alan Chen[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.

    • Marked as answer by Alan_chen Wednesday, September 28, 2011 3:03 AM
    Sunday, September 18, 2011 9:07 AM
  • Hi,

    I am writing to check the status of the issue on your side. Would you mind letting us know the result of the suggestions?

    If you need further assistance, please feel free to let me know. I will be more than happy to be of assistance.

    Have a nice day.


    Alan Chen[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.

    Monday, September 26, 2011 3:14 AM