c# - LINQ 中的 GroupBy 与字符串 [] 数组

标签 c# linq windows-phone

我正在开发数据库应用程序,该应用程序通过其标签显示清晰的交易报告。

这是我的旧数据库

Title      Amount       TagsReport (string[] array)
Food       5            "Hotel","Friends"
Food       6            "Hotel"
Family     8            "Hotel","Mobile"
Family     9            "Electricity"
Food       8            "Party"

我希望生成如下报告:

期望的输出:

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

57%            Family            17
                 Hotel             8
                 Mobile            8
                 Electricity       9

我对 LINQ 的了解不够。所以我在寻找完美解决方案的过程中遇到了很多麻烦。

但是我发现这段代码可以使用,

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.TagsReport.Select(tag => new
    {
        Tag = tag,
        Amount = item.Amount
    }))
    .GroupBy(tagAmount => tagAmount.Tag)
    .Select(tagAmountGroup => new
    {
        Tag = tagAmountGroup.Key,
        TotalAmount = tagAmountGroup.Sum(tagAmount => tagAmount.Amount)
    })
});

我得到了我想要的输出。

现在我们已经从数据库中删除了标题。所以 db 现在就像,

Amount       TagsReport (string[] array)
5            "Hotel","Friends"
6            "Hotel"
8            "Hotel","Mobile"
9            "Electricity"
8            "Party"

现在我将单独按标签对所有交易进行分组,如下所示:

Amount       Title
19           Hotel
5            Friends
8            Mobile
9            Electricity
8            Party

请帮助我。

最佳答案

有了新要求,它变得更加简单。

如果你的数据是这样开始的:

var ReportListQuery = new []
{
    new { Amount = 5, TagsReport = new [] { "Hotel", "Friends" } },
    new { Amount = 6, TagsReport = new [] { "Hotel" } },
    new { Amount = 8, TagsReport = new [] { "Hotel", "Mobile" } },
    new { Amount = 9, TagsReport = new [] { "Electricity" } },
    new { Amount = 8, TagsReport = new [] { "Party" } },
};

那么这是你需要的查询:

var ReportingData =
    from report in ReportListQuery
    from tag in report.TagsReport
    group report.Amount by tag into gtags
    select new
    {
        Title = gtags.Key,
        Amount = gtags.Sum(),
    };

我得到的结果是这样的:

result

附带说明一下,在您的原始查询中,您使用的是百分比数字的整数数学。这是行不通的,每个数字都会给你零。您需要更改此行:

Percentage = Math.Round((itemGroup.Sum(item => item.Amount) / MonthExpense) * 100),

为此:

Percentage = Math.Round(itemGroup.Sum(item => item.Amount) * 100.0m / MonthExpense),

这将使它使用 decimal 而不是 int 并为您提供正确的结果。

关于c# - LINQ 中的 GroupBy 与字符串 [] 数组,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/28845397/

相关文章:

c# - 计算每行文本文件的制表符数

c# - c#解析Json数据转xml

c# - Orderby() 没有正确排序数字 c#

c# - 有没有办法在 Entity Framework 查询的 `Where` 部分进行转换?

c# - 从任务回调到主线程

javascript - 如何在 PhoneGap 的 windows 平台上实现 map (Google 或 Bing)?

java - 在 Java 中处理重复的正则表达式组名称(C# 翻译)

c# - Lambda 表达式未返回预期的 MemberInfo

c# - 为什么后台线程中的图形操作会阻塞主 UI 线程中的图形操作?

c# - 从另一类 Windows Phone 访问页面的按钮控件?