包含投影不起作用

发布于 2024-12-12 05:49:33 字数 1146 浏览 0 评论 0原文

我有这个查询

    var test = context.Assignments
        .Include(a => a.Customer)
        .Include(a => a.Subscriptions)
        .Select(a => new AssignmentWithSubscriptionCount { SubscriptionCount = a.Subscriptions.Count(), Assignment = a })
        .ToList();

    var name = test.First().Assignment.Customer.Name;

它无法急切地加载客户,我在 stackoverflow 上看到了类似的问题,看起来你不能将投影与 include 一起使用。但我还没有找到解决我的问题的方法..有人吗?

编辑:这是一个可以工作的投影的急切负载,它比上面的示例更复杂,所以我一生都无法理解出了什么问题,谢谢。

    var test = context.PublicationStateGroups
        .Include(p => p.PublicationStates.Select(ps => ps.AllowedPublicationStateActions.Select(aps => aps.PublicationStateAction)))
        .Select(psg => new StateAndGroupInfo
        {
            ShowReport = psg.PublicationStates.Any(p => p.PublicationStateReportTypeId.HasValue),
            Actions = psg.PublicationStates.SelectMany(state => state.AllowedPublicationStateActions)
                .Select(a => a.PublicationStateAction)
                .Distinct()
        }).ToList();

    var eagerTest = test.First().Actions.First().Name;

I have this query

    var test = context.Assignments
        .Include(a => a.Customer)
        .Include(a => a.Subscriptions)
        .Select(a => new AssignmentWithSubscriptionCount { SubscriptionCount = a.Subscriptions.Count(), Assignment = a })
        .ToList();

    var name = test.First().Assignment.Customer.Name;

It failes to eagerly load Customer, I've seen similar problems here on stackoverflow and it looks like you cant use projections with include. But I have not found a solution to my problem.. Anyone?

edit: Here is a eager load with projection that work, its more complex than the example above so I cant for my life understand whats wrong, thanks.

    var test = context.PublicationStateGroups
        .Include(p => p.PublicationStates.Select(ps => ps.AllowedPublicationStateActions.Select(aps => aps.PublicationStateAction)))
        .Select(psg => new StateAndGroupInfo
        {
            ShowReport = psg.PublicationStates.Any(p => p.PublicationStateReportTypeId.HasValue),
            Actions = psg.PublicationStates.SelectMany(state => state.AllowedPublicationStateActions)
                .Select(a => a.PublicationStateAction)
                .Distinct()
        }).ToList();

    var eagerTest = test.First().Actions.First().Name;

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

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

发布评论

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

评论(2

浮光之海 2024-12-19 05:49:33

这是设计Include 不适用于具有投影或自定义联接的场景。

This is by design. Include is not for scenarios with projection or custom joins.

烟凡古楼 2024-12-19 05:49:33

将客户添加到您的投影中:

var test = context.Assignments
    .Select(a => new AssignmentWithSubscriptionCount
    {
        SubscriptionCount = a.Subscriptions.Count(),
        Assignment = a,
        Customer = a.Customer
    });

var name = test.First().Customer.Name;

EF 上下文可能会确保自动填充 Assignment.Customer

编辑

如果您不想或无法更改AssignmentWithSubscriptionCount类,您还可以投影到匿名类型,然后将内存中的结果复制到此类中:

var test = context.Assignments
    .Select(a => new
    {
        SubscriptionCount = a.Subscriptions.Count(),
        Assignment = a,
        Customer = a.Customer
    });

test.ToList() // executes query
    .Select(o =>
    {
        o.Assignment.Customer = o.Customer;
        return new AssignmentWithSubscriptionCount
        {
            SubscriptionCount = o.SubscriptionCount,
            Assignment = o.Assignment
        }
    });

另一种选择是显式加载(但每个加载的Assignment需要一次额外的往返)。

Add the customer to your projection:

var test = context.Assignments
    .Select(a => new AssignmentWithSubscriptionCount
    {
        SubscriptionCount = a.Subscriptions.Count(),
        Assignment = a,
        Customer = a.Customer
    });

var name = test.First().Customer.Name;

The EF context will probably ensure that Assignment.Customer gets populated automatically.

Edit

If you don't want or can't change the AssignmentWithSubscriptionCount class you can also project into an anonymous type and then copy the result in memory into this class:

var test = context.Assignments
    .Select(a => new
    {
        SubscriptionCount = a.Subscriptions.Count(),
        Assignment = a,
        Customer = a.Customer
    });

test.ToList() // executes query
    .Select(o =>
    {
        o.Assignment.Customer = o.Customer;
        return new AssignmentWithSubscriptionCount
        {
            SubscriptionCount = o.SubscriptionCount,
            Assignment = o.Assignment
        }
    });

Another option is explicite loading (requires one additional roundtrip per loaded Assignment though).

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