ObjectCollection.Where(o => o.Related == EntityObject): “Unable to create a constant value”

巧了我就是萌 提交于 2019-12-24 21:17:53

问题


Given an EntityObject, I'd like an object-oriented way find all related items as part of my data-source query.

The following produces the correct output, but brings all the rows over the wire to do it.

Parent.                                             // EntityObject
Children.                                           // EntityCollection
Where(o => o.Gender == 'm').                        // IEnumerable (local!)
OrderBy(o => o.Age).                                // IOrderedEnumerable
Skip(pages * pageSize).Take(pageSize);              // (Inefficient paging!)

I need to support a UI with this (filter using other criteria, sort, and paginate before returning results over the wire). I reordered to leverage Queryable:

Repository.                                         // Repository
Children.                                           // ObjectSet
Where(o => o.Parent == Parent && o.Gender == 'm').  // ObjectQuery, runtime error
OrderBy(o => o.Age).                                // IOrderedQueryable
Skip(pages * pageSize).Take(pageSize);

but this yields the error:

Unable to create a constant value of type 'DataModel.Parent'. Only primitive types ('such as Int32, String, and Guid') are supported in this context.

Is there a natural, object-oriented way to query on this relation, using Linq to Entities? Or do I necessarily fall-back to SQL for this?

I thought for a moment that CreateSourceQuery was going to be the answer, but it can't be applied to an EntityObject.


回答1:


I can't do a test for what I'm saying, but I think that you get the error because EF doesn't know how to translate o.Parent == Parent into a SQL statement. Try compare the Ids of the two parents.. o.Parent.Id == Parent.Id



来源:https://stackoverflow.com/questions/11914729/objectcollection-whereo-o-related-entityobject-unable-to-create-a-cons

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!