如何在 Property Count = Something 的情况下使用 LINQ Lambda

发布于 2024-10-21 09:21:54 字数 461 浏览 1 评论 0原文

我有一个实体对象 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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

阪姬 2024-10-28 09:21:54

Category 也是 EF 跟踪的实体吗? EF似乎不这么认为。您使用 POCO 吗?通常,一对多的导航属性需要用 ICollection表示,而不是 IEnumerable。

基本上,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 with ICollection<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.

旧人九事 2024-10-28 09:21:54

尝试

var unassigned = db.Items.Categories.Where(c=>c.Count() ==0);

Try

var unassigned = db.Items.Categories.Where(c=>c.Count() ==0);
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文