实体框架包含与哪里

发布于 2024-12-09 07:54:34 字数 1168 浏览 0 评论 0原文

我的数据库结构是这样的:一个OptiUser通过IdentityMap表属于多个UserGroup,这是一个匹配表(多对多)附加到它的一些附加属性。每个UserGroup都有多个OptiDashboard

我有一个 GUID 字符串,用于标识特定用户(此代码中的 wlid)。我想要获取由 wlid 标识的用户的所有 OptiDashboardsIEnumerable

这两个 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 UserGroups through the IdentityMap table, which is a matching table (many to many) with some additional properties attached to it. Each UserGroup has multiple OptiDashboards.

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 技术交流群。

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

发布评论

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

评论(3

千鲤 2024-12-16 07:54:34

您可能误解了 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 the Include 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.

妄想挽回 2024-12-16 07:54:34

我认为 Include 将呈现为 joins,您将能够访问用户对象中这些表中的数据(急切加载属性)。

Any 查询将呈现为 exists 并且不会使用其他表中的信息加载用户对象。

为了获得最佳性能,如果您不需要附加信息,请使用 Any 查询

I think the Include will render as joins 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 as exists 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

计㈡愣 2024-12-16 07:54:34

正如已经指出的那样,第一个选项几乎肯定会表现更好,因为它会检索更少的信息。除此之外,我想指出,您也可以这样编写查询:

var dashboards =
    from u in db.OptiUsers where u.WinLiveIDToken == wlid
    from im in u.IdentityMaps
    from d in im.UserGroup.OptiDashboards
    select d;

我希望上面的执行方式与第一个选项类似,但您可能(或可能不)更喜欢上面的形式。

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:

var dashboards =
    from u in db.OptiUsers where u.WinLiveIDToken == wlid
    from im in u.IdentityMaps
    from d in im.UserGroup.OptiDashboards
    select d;

I would expect the above to perform similarly to the first option, but you may (or may not) prefer the above form.

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