如何在 Property Count = Something 的情况下使用 LINQ Lambda
我有一个实体对象 Item,看起来像这样..
public class Item()
{
IEnumerable<Category> Categories { get; set; }
}
项目列表
var unassigned = db.Items.Where(i => i.Categories.Count() == 0);
我正在尝试获取计数为 0或
var unassigned = db.Items.Where(i => i.Categories.Any());
但都抛出错误的 ... “LINQ to Entities 不支持指定的类型成员‘类别’。仅支持初始值设定项、实体成员和实体导航属性。”
这个错误告诉我什么以及如何查询我要查找的内容?
I have an Entity object, Item, that looks like this..
public class Item()
{
IEnumerable<Category> Categories { get; set; }
}
and I am trying to get a list of items that have a 0 count
var unassigned = db.Items.Where(i => i.Categories.Count() == 0);
or
var unassigned = db.Items.Where(i => i.Categories.Any());
but both throw the error...
"The specified type member 'Categories' is not supported in LINQ to Entities. Only initializers, entity members, and entity navigation properties are supported."
What is this error telling me and how can I query for what I am looking for?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
Category
也是 EF 跟踪的实体吗? EF似乎不这么认为。您使用 POCO 吗?通常,一对多的导航属性需要用 ICollection基本上,EF 表示它不知道如何将 db.Items.Where(i => i.Categories.Count() == 0) 转换为 SQL,因为它不确定 Category 位于什么位置与数据库的关系。
您也可能需要在 EF 调用中包含("Categories"),但我认为您遇到的问题比这更根本。
Is
Category
an entity as well that is tracked by EF? EF doesn't seem to think it is. Are you using POCOs? Typically navigation properties that are 1-to-many need to be represented withICollection<T>
not IEnumerable.Basically EF is saying it doesn't know how to turn
db.Items.Where(i => i.Categories.Count() == 0)
into SQL, because it's unsure what Categories is in relation to the database.It's also possible you will need to
Include("Categories")
on the EF call, but I think you have more fundamental problems than this.尝试
Try