ObjectCollection.Where(o => o.Related == EntityObject):“无法创建常量值” [英] ObjectCollection.Where(o => o.Related == EntityObject): "Unable to create a constant value"
问题描述
下面的代码产生了正确的输出,但是将所有行都放在线上。
父母。 // EntityObject
儿童。 // EntityCollection
其中(o => o.Gender =='m')。 // IEnumerable(local!)
OrderBy(o => o.Age)。 // IOrderedEnumerable
Skip(pages * pageSize).Take(pageSize); //(效率低下的页面)
我需要支持一个UI(使用其他条件过滤,排序和分页,然后通过电线返回结果)。我重新订购以利用Queryable:
资源库。 // Repository
儿童。 // ObjectSet
其中(o => o.Parent == Parent&& o.Gender =='m')。 // ObjectQuery,运行时错误
OrderBy(o => o.Age)。 // IOrderedQueryable
Skip(pages * pageSize).Take(pageSize);
但这会产生错误:
无法创建DataModel.Parent类型的常量值。在这种情况下,只支持原始类型(如Int32,String和Guid)。
有一种自然的面向对象的方式查询这个关系,使用Linq to Entities?或者我必须回退到SQL?
我想了一会儿, CreateSourceQuery 将成为答案,但不能应用于EntityObject。
我不能对我在说什么进行测试,但我认为你得到错误,因为EF不知道如何翻译o.Parent == Parent 转换成SQL语句。尝试比较两个父母的身份。
o.Parent.Id == Parent.Id
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.
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
这篇关于ObjectCollection.Where(o => o.Related == EntityObject):“无法创建常量值”的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!