简体   繁体   English

将方法转换为存储的表达式

[英]Translating method into stored expression

I just wanted to make my code and linq queries to look better, and made an extension method for my Bank entity, as I would like to make queries look something like this 我只是想使我的代码和linq查询看起来更好,并为我的Bank实体创建了扩展方法,因为我想使查询看起来像这样

db.Banks.Where(x => x.SomeBooleanParam).Select(x => x.ToSummary())

But I run into exception 但是我遇到了例外

Linq-to-Entities does not recognize the method 'XXSummary ToSummary(XXModels.Bank)' method, and this method cannot be translated into a store expression. Linq-to-Entities无法识别方法'XXSummary ToSummary(XXModels.Bank)',并且该方法无法转换为商店表达式。

My question: is it possible to somehow modify my ToSummary() function, so it is possible to use it on entities in queries? 我的问题:是否可以以某种方式修改ToSummary()函数,以便可以在查询中的实体上使用它? I have provided how my db would look like in JSON format, how simple select looks like and how summary looks like. 我提供了我的数据库以JSON格式显示的样子,select的简单外观和摘要的外观。 I have read, that the function must return something like Expression<Func<somethingHere, bool>> but I have never understood it.. 我读过,该函数必须返回类似Expression<Func<somethingHere, bool>>但我从未理解过。

//How it looks in the DB
Items: [
    {Id: 1, Name: "Item1"},
    {Id: 2, Name: "Item2"},
    {Id: 3, Name: "Item3"}
]

Banks: [
    {Id: 1, Name: Bank1, SomeBooleanParam: true}
]

BankChanges: [
    {Id: 1, BankId: 1},
    {Id: 2, BankId: 1}
]

BankItems: [
    {Id: 1, ItemId: 1, BankChangeId: 1, Added: 10, Generated: 0},
    {Id: 2, ItemId: 2, BankChangeId: 1, Added: 1, Generated: 1}
    {Id: 3, ItemId: 1, BankChangeId: 2, Added: -10, Generated: 0},
    {Id: 4, ItemId: 2, BankChangeId: 2, Added: 3, Generated: 0},
    {Id: 5, ItemId: 3, BankChangeId: 2, Added: 7, Generated: 0}
]

// How simple select looks like, by including everything
simpleSelect:
[{
    Id: 1,
    Name: Bank1,
    BankChanges: [
        {
            Id: 1,
            BankItems: [
                {
                    Id: 1,
                    Item: {Id: 1, Name: "Item1"},
                    BankChangeId: 1,
                    Added: 10,
                    Generated: 0
                },
                {
                    Id: 2,
                    Item: {Id: 2, Name: "Item2"},
                    BankChangeId: 1,
                    Added: 1,
                    Generated: 1
                }
            ]
        },
        {
            Id: 2,
            BankItems: [
                {
                    Id: 3,
                    Item: {Id: 1, Name: "Item1"},
                    Added: -10,
                    Generated: 0
                },
                {
                    Id: 4,
                    Item: {Id: 2, Name: "Item2"},
                    Added: 3,
                    Generated: 0
                },
                {
                    Id: 5,
                    Item: {Id: 3, Name: "Item3"},
                    Added: 7,
                    Generated: 0
                }
            ]
        }
    ]
}]
// How summary looks like
summary:
[{
    Id: 1,
    Name: Bank1,
    ItemSummaries: [
        // Note: No Item1, because total of Item1 = 0 or less
        {
            Item: "Item2",
            TotalAdded: 4,
            TotalGenerated: 1,
            Total: 5
        },
        {
            Item: "Item3",
            TotalAdded: 7,
            TotalGenerated: 0,
            Total: 7
        }
    ]
}]

public static class BankExtensions {
    public static Summary ToSummary(this Bank bank)
    {
        return new Summary 
        {
            Id = bank.Id,
            Name = bank.Name,
            ItemSummaries = bank.BankChanges
                                .SelectMany(x => x.BankItems)
                                .GroupBy(x => x.Item)
                                .Select(x => new ItemSummary
                                {
                                    Item = x.Key.Name,
                                    TotalAdded = x.Sum(y => y.Added),
                                    TotalGenerated = x.Sum(y => y.Generated),
                                    Total = x.Sum(y => y.Added) + x.Sum(y => y.Generated)
                                })
                                .Where(x => x.Total > 0)
        };
    }
}

Something like: 就像是:

public static class BankExtensions
{
    public static IQueryable<Summary> ToSummary(this IQueryable<Bank> b)
    {
        return b.Select(bank => new Summary
            {
                Id = bank.Id,
                Name = bank.Name,
                ItemSummaries = bank.BankChanges
                                    .SelectMany(x => x.BankItems)
                                    .GroupBy(x => x.Item)
                                    .Select(x => new ItemSummary
                                    {
                                        Item = x.Key.Name,
                                        TotalAdded = x.Sum(y => y.Added),
                                        TotalGenerated = x.Sum(y => y.Generated),
                                        Total = x.Sum(y => y.Added) + x.Sum(y => y.Generated)
                                    })
                                    .Where(x => x.Total > 0)
            }
        );
    }
}

And then you use it like: 然后像这样使用它:

db.Banks.Where(x => x.SomeBooleanParam).ToSummary()

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM