简体   繁体   English

如何在LINQ中使用分组?

[英]How to use Grouping in LINQ?

I just wish to make my question clear. 我只想澄清我的问题。 This is my database . 这是我的数据库

Title      Amount       Tags
Food       5            Hotel,Friends
Food       6            Hotel
Family     8            Hotel,Mobile
Family     9            Electricity
Food       8            Party

I wish to generate the report like below: 我希望生成如下报告:

Desired Output: 所需输出:

Percentage     Title             Amount
53%            Food              19
                  Hotel             11
                  Friends           5
                  Party             8

57%            Family            17
                 Hotel             8
                 Mobile            8
                 Electricity       9

I don't have enough knowledge on LINQ. 我对LINQ没有足够的了解。 So I face lot of trouble in this finding a perfect solution. 因此,在寻找一个完美的解决方案时,我面临很多麻烦。

The code I use right now, 我现在使用的代码

var ReportingData = ReportListQuery.Where(Item => Item.Category == "expense")
                                   .GroupBy(x => new { x.Title, x.TagsReport })
                                   .Select(y => new
                                         {
                                             Percentage = Math.Round((y.Sum(x => x.Amount) / MonthExpense) * 100),
                                             ExpenseTitle = y.First().Title,
                                             ExpenseCalculation = y.Sum(x => x.Amount)
                                          });

And here's the output for my code: 这是我的代码的输出:

Output: 输出:

Percentage     Title      Amount  
53%            Food       19
57%            Family     17

Thanks in advance. 提前致谢。 Please help me. 请帮我。 :-( :-(

As I understand your question, you don't know how to get the results per tag, right? 据我了解您的问题,您不知道如何获取每个标签的结果,对吗?

First of all, I strongly recommend to use meaningful names in complex LINQ queries instead of x, y, etc. Also it seems to me that you group by TagsReport for which I don't see a reason. 首先,我强烈建议在复杂的LINQ查询中使用有意义的名称,而不要使用x,y等。在我看来,您还按TagReport分组,但我没有理由。 And finally, instead of y.First().Title you can just use the group key. 最后,不用y.First()。Title即可使用组密钥。 After applying these recommendations, your simplified query looks like this: 应用这些建议后,简化的查询如下所示:

var ReportingData = ReportListQuery
    .Where(Item => Item.Category == "expense")
    .GroupBy(item => item.Title)
    .Select(itemGroup => new
    {
        Percentage = Math.Round((itemGroup.Sum(item => item.Amount) / MonthExpense) * 100),
        ExpenseTitle = itemGroup.Key,
        ExpenseCalculation = itemGroup.Sum(item => item.Amount)
    });

Now, to add the results per tag, you could generate another property on your anonymous type containing the list of tag amounts: 现在,要为每个标签添加结果,您可以在包含标签数量列表的匿名类型上生成另一个属性:

var ReportingData = ReportListQuery
    .Where(Item => Item.Category == "expense")
    .GroupBy(item => item.Title)
    .Select(itemGroup => new
    {
        Percentage = Math.Round((itemGroup.Sum(item => item.Amount) / MonthExpense) * 100),
        ExpenseTitle = itemGroup.Key,
        ExpenseCalculation = itemGroup.Sum(item => item.Amount)
        TotalTagAmounts = itemGroup
            .SelectMany(item => item.Tags.Select(tag => new { Tag = tag, Amount = amount})
            .GroupBy(tagAmount => tagAmount.Tag)
            .Select(tagAmountGroup => new 
            { 
                Tag = tagAmountGroup.Key, 
                TotalAmount = tagAmountGroup.Sum(tagAmount => tagAmount.Amount)
            })
    });

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

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