简体   繁体   English

如何按子实体的属性分组并获取 linq 中子实体的总和?

[英]How to group by property of child entity and get sum of child entity in linq?

I need to group data by child entity and get sum of child entity.我需要按子实体对数据进行分组并获得子实体的总和。 So, I tried this linq:所以,我尝试了这个 linq:

context.Parent.Include(p => p.Child).GroupBy(p => p.Child.Type).Select(g => g.Sum(p => p.Child.Amount));

Above linq results in an error.以上 linq 导致错误。 However, if I were to get sum of parent entity it will work.但是,如果我要获得父实体的总和,它将起作用。

context.Parent.Include(p => p.Child).GroupBy(p => p.Child.Type).Select(g => g.Sum(p => p.Amount));

Why cannot I get the sum of child entity?为什么我不能得到子实体的总和?

It is limitation.这是限制。 After GroupBy you cannot use navigation properties.在 GroupBy 之后,您不能使用导航属性。 Also after GroupBy all Includes are completely ignored, so omit them.同样在 GroupBy 之后,所有的 Includes 都被完全忽略,所以省略它们。

You query can be written in the following way:您的查询可以写成以下方式:

var query = 
    from p in context.Parent
    group p.Child by p.Child.Type into g
    select new 
    {
        Type = g.Key,
        Sum = g.Sum(c => c.Amount)
    };

Lambda syntax variant: Lambda 语法变体:


var query = context.Parent
    .GroupBy(p => p.Child.Type, p => p.Child)
    .Select(g => new 
    {
       Type = g.Key, 
       Sum = g.Sum(c => c.Amount)
    });

Apparently, in your model you have a sequence of Parents .显然,在您的Parents中,您有一系列 parents 。 Every Parent has a property Child .每个Parent都有一个属性Child Every Child has two properties: Amount and Type .每个Child都有两个属性: AmountType

You forgot to tell us your specification, and gave us some code that does not do what you want.您忘记告诉我们您的规范,并给了我们一些不符合您要求的代码。 So it is a bit difficult for us to know what you want.所以我们很难知道你想要什么。

I think that you want to make groups of Parents that have the same value for Parent.Child.Type .我认为您希望创建具有相同值的Parent.Child.Type Parents From every Parent in the group you want to take the value of Parent.Child.Amount and sum theses values.从组中的每个 Parent 中获取Parent.Child.Amount的值并对这些值求和。

You are right, for this you should use one of the overloads of Enumerable.GroupBy.你是对的,为此你应该使用 Enumerable.GroupBy 的重载之一。 My advice would be to use the overload that has a parameter resultSelector:我的建议是使用具有参数 resultSelector 的重载:

// make groups of Parents with same value of Parents.Child.Type:
var result = dbContext.Parents.GroupBy(parent => parent.Child.Type,

// parameter resultSelector: for every Type, and all Parents that have this Child.Type
// make one new:
(type, parentsWithThisChildType) => new
{
    // do you want to know the type?
    ChildType = type,

    // from all parents in this group, select the Parent.Child.Amount and Sum them:
    TotalChildAmounts = parentsWithThisChildType
        .Select(parent => parent.Child.Amount)
        .Sum(),
})

In words: from your sequence of Parents make groups of Parents that have the same value for Parent.Child.Type.换句话说:从您的父母序列中,创建具有相同 Parent.Child.Type 值的父母组。 From every group make one object as follows:从每组中制作一个 object,如下所示:

  • Is desired, take the common Type of all Parent.Child.Type in this group可取,取本组所有Parent.Child.Type的通用Type
  • from every Parent in the group, select the value of Parent.Child.Amount.来自组中的每个 Parent,select Parent.Child.Amount 的值。
  • Sum all values of this Amound, and put the result in TotalChildAmounts将这个 Amound 的所有值相加,并将结果放入 TotalChildAmounts

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

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