如何查询对象集并在同一查询中过滤附加的实体集合?

发布于 2024-10-11 17:43:19 字数 1397 浏览 2 评论 0原文

我第一次使用实体框架,注意到实体对象返回实体集合。

DBEntities db = new DBEntities();
db.Users; //Users is an ObjectSet<User>
User user = db.Users.Where(x => x.Username == "test").First(); //Is this getting executed in the SQL or in memory?
user.Posts; //Posts is an EntityCollection<Post>
Post post = user.Posts.Where(x => x.PostID == "123").First(); //Is this getting executed in the SQL or in memory?

ObjectSet 和 EntityCollection 都实现 IQueryable 吗?我希望他们这样做,以便我知道查询是在数据源而不是在内存中执行的。

编辑:所以显然 EntityCollection 不会,而 ObjectSet 会。这是否意味着我使用这段代码会更好?

    DBEntities db = new DBEntities();
    User user = db.Users.Where(x => x.Username == "test").First(); //Is this getting executed in the SQL or in memory?
    Post post = db.Posts.Where(x => (x.PostID == "123")&&(x.Username == user.Username)).First(); // Querying the object set instead of the entity collection.

另外,ObjectSet 和 EntityCollection 之间有什么区别?它们不应该是一样的吗?

提前致谢!

编辑:抱歉,我是新手。我正在尝试理解。附加的 EntityCollections 是延迟加载的,因此如果我访问它们,那么内存就会填充它们。我很好奇这个查询是否更符合我的要求,而不是像上次编辑中那样对对象集进行两次查询:

DBEntities db = new DBEntities();
User user = (from x in db.Users
             from y in x.Posts
             where x.Username == "test"
             where y.PostID == 123
             select x).First();

I am using Entity Framework for the first time and noticed that the entities object returns entity collections.

DBEntities db = new DBEntities();
db.Users; //Users is an ObjectSet<User>
User user = db.Users.Where(x => x.Username == "test").First(); //Is this getting executed in the SQL or in memory?
user.Posts; //Posts is an EntityCollection<Post>
Post post = user.Posts.Where(x => x.PostID == "123").First(); //Is this getting executed in the SQL or in memory?

Do both ObjectSet and EntityCollection implement IQueryable? I am hoping they do so that I know the queries are getting executed at the data source and not in memory.

EDIT: So apparently EntityCollection does not while ObjectSet does. Does that mean I would be better off using this code?

    DBEntities db = new DBEntities();
    User user = db.Users.Where(x => x.Username == "test").First(); //Is this getting executed in the SQL or in memory?
    Post post = db.Posts.Where(x => (x.PostID == "123")&&(x.Username == user.Username)).First(); // Querying the object set instead of the entity collection.

Also, what is the difference between ObjectSet and EntityCollection? Shouldn't they be the same?

Thanks in advance!

EDIT: Sorry, I'm new to this. I'm trying to understand. Attached EntityCollections are lazy loaded, so if I access them then memory is populated with them. Rather than doing two querys to the object sets like in my last edit, I am curious if this query would be more what I was after:

DBEntities db = new DBEntities();
User user = (from x in db.Users
             from y in x.Posts
             where x.Username == "test"
             where y.PostID == 123
             select x).First();

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

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

发布评论

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

评论(2

留一抹残留的笑 2024-10-18 17:43:19

ObjectSet确实实现了 IQueryable,但 EntityCollection< /code>没有。

不同之处在于 ObjectSet 用于直接查询(这就是它实现该接口的原因)。另一方面,EntityCollection 用于结果集的“多”端,通常在 ObjectSet 上完成的查询中返回。因此,它实现 IEnumerable,但不实现 IQueryable(因为它已经是查询的填充结果)。

ObjectSet<T> does implement IQueryable<T>, but EntityCollection<T> does not.

The difference is that ObjectSet<T> is meant to be used for querying directly (which is why it does implement the interface). EntityCollection<T>, on the other hand, is used for the "many" end of a result set, typically returned in a query done on an ObjectSet<T>. As such, it impelments IEnumerable<T>, but not IQueryable<T> (as it's already the populated results of a query).

握住你手 2024-10-18 17:43:19

我几乎准备好说是的,他们都这样做。幸运的是,我首先检查了文档

EntityCollection 不实现 IQueryable。

至于区别,ObjectSet 表示从数据库中的表生成的对象。 EntityCollection 表示一对多或多对多关系的“多”侧实体对象的集合。

I was almost ready to say yes, they both do. Luckily I check the documentation first.

EntityCollection does not implement IQueryable.

As for the difference, ObjectSet<TEntity> represents the the objects generated from a table in a database. EntityCollection<TEntity> represents a collection of entity objects on the 'Many' side of One to Many or Many to Many relationship.

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