如何查询对象集并在同一查询中过滤附加的实体集合?
我第一次使用实体框架,注意到实体对象返回实体集合。
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 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
ObjectSet
确实实现了IQueryable
,但EntityCollection< /code>
没有。
不同之处在于
ObjectSet
用于直接查询(这就是它实现该接口的原因)。另一方面,EntityCollection
用于结果集的“多”端,通常在ObjectSet
上完成的查询中返回。因此,它实现IEnumerable
,但不实现IQueryable
(因为它已经是查询的填充结果)。ObjectSet<T>
does implementIQueryable<T>
, butEntityCollection<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 anObjectSet<T>
. As such, it impelmentsIEnumerable<T>
, but notIQueryable<T>
(as it's already the populated results of a query).我几乎准备好说是的,他们都这样做。幸运的是,我首先检查了文档。
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.