实体框架包含与哪里
我的数据库结构是这样的:一个OptiUser
通过IdentityMap
表属于多个UserGroup
,这是一个匹配表(多对多)附加到它的一些附加属性。每个UserGroup
都有多个OptiDashboard
。
我有一个 GUID 字符串,用于标识特定用户(此代码中的 wlid
)。我想要获取由 wlid
标识的用户的所有 OptiDashboards
的 IEnumerable
。
这两个 Linq-to-Entities 查询哪一个最有效?它们在后端的运行方式是否相同?
另外,我可以将选项 2 的 Include
语句缩短为 .Include("IdentityMaps.UserGroup.OptiDashboards")
吗?
using (OptiEntities db = new OptiEntities())
{
// option 1
IEnumerable<OptiDashboard> dashboards = db.OptiDashboards
.Where(d => d.UserGroups
.Any(u => u.IdentityMaps
.Any(i => i.OptiUser.WinLiveIDToken == wlid)));
// option 2
OptiUser user = db.OptiUsers
.Include("IdentityMaps")
.Include("IdentityMaps.UserGroup")
.Include("IdentityMaps.UserGroup.OptiDashboards")
.Where(r => r.WinLiveIDToken == wlid).FirstOrDefault();
// then I would get the dashboards through user.IdentityMaps.UserGroup.OptiDashboards
// (through foreach loops...)
}
My database structure is this: an OptiUser
belongs to multiple UserGroup
s through the IdentityMap
table, which is a matching table (many to many) with some additional properties attached to it. Each UserGroup
has multiple OptiDashboard
s.
I have a GUID string which identifies a particular user (wlid
in this code). I want to get an IEnumerable
of all of the OptiDashboards
for the user identified by wlid
.
Which of these two Linq-to-Entities queries is the most efficient? Do they run the same way on the back-end?
Also, can I shorten option 2's Include
statements to just .Include("IdentityMaps.UserGroup.OptiDashboards")
?
using (OptiEntities db = new OptiEntities())
{
// option 1
IEnumerable<OptiDashboard> dashboards = db.OptiDashboards
.Where(d => d.UserGroups
.Any(u => u.IdentityMaps
.Any(i => i.OptiUser.WinLiveIDToken == wlid)));
// option 2
OptiUser user = db.OptiUsers
.Include("IdentityMaps")
.Include("IdentityMaps.UserGroup")
.Include("IdentityMaps.UserGroup.OptiDashboards")
.Where(r => r.WinLiveIDToken == wlid).FirstOrDefault();
// then I would get the dashboards through user.IdentityMaps.UserGroup.OptiDashboards
// (through foreach loops...)
}
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(3)
您可能误解了
Include
函数的实际用途。选项 1 纯粹是一种查询语法,对实体框架返回的内容没有影响。选项 2 使用Include
函数指示实体框架在返回查询结果时立即从数据库中获取相关行。因此,选项 1 将导致一些联接,但查询的“选择”部分将仅限于
OptiDashboards
表。选项 2 也会导致连接,但在这种情况下,它将返回所有包含的表的结果,这显然会带来更多的性能损失。但与此同时,结果将包括您需要的所有相关实体,从而避免[可能]需要更多的数据库往返。
You may be misunderstanding what the
Include
function actually does. Option 1 is purely a query syntax which has no effect on what is returned by the entity framework. Option 2, with theInclude
function instructs the entity framework to Eagerly Fetch the related rows from the database when returns the results of the query.So option 1 will result in some joins, but the "select" part of the query will be restricted to the
OptiDashboards
table.Option 2 will result in joins as well, but in this case it will be returning the results from all the included tables, which obviously is going to introduce more of a performance hit. But at the same time, the results will include all the related entities you need, avoiding the [possible] need for more round-trips to the database.
我认为
Include
将呈现为joins
,您将能够访问用户对象中这些表中的数据(急切加载属性)。Any
查询将呈现为exists
并且不会使用其他表中的信息加载用户对象。为了获得最佳性能,如果您不需要附加信息,请使用
Any
查询I think the
Include
will render asjoins
an you will the able to access the data from those tables in you user object (Eager Loading the properties).The
Any
query will render asexists
and not load the user object with info from the other tables.For best performance if you don't need the additional info use the
Any
query正如已经指出的那样,第一个选项几乎肯定会表现更好,因为它会检索更少的信息。除此之外,我想指出,您也可以这样编写查询:
我希望上面的执行方式与第一个选项类似,但您可能(或可能不)更喜欢上面的形式。
As has already been pointed out, the first option would almost certainly perform better, simply because it would be retrieving less information. Besides that, I wanted to point out that you could also write the query this way:
I would expect the above to perform similarly to the first option, but you may (or may not) prefer the above form.