从匿名类型返回索引属性

时间:2015-05-31 05:42:58

标签: .net vb.net linq anonymous-types

我的LINQ查询返回一年中每个月的总计,我将其暴露为匿名类型的12个属性(名为January,February,March ...)。这使得无法遍历十二个值。有没有办法返回一个包含所有12个值的索引属性?

这就是我所拥有的:

Dim MyTotals = Aggregate CostRow In AllTargetRows _
               Into January = Sum(CostRow.January), 
                    February = Sum(CostRow.February), 
                    March = Sum(CostRow.March), _
                    ...
                    Total = Sum(CostRow.Total)

这就是我想要实现的目标(编译器不喜欢这个):

Dim MyTotals = Aggregate CostRow In AllTargetRows _
               Into Values = {Sum(CostRow.January), 
                              Sum(CostRow.February), 
                              Sum(CostRow.March), _
                              ...
                              },
                    Total = Sum(CostRow.Total)

然后我就能做MyTotals.Values(i)种事情。

1 个答案:

答案 0 :(得分:0)

我想到的第一件事就是使用Aggregate函数来填充字典。像这样:

Dim MyTotals =
    AllTargetRows
    .Aggregate(
        new Dictionary<string, int>()
            {
                { "January", 0 },
                { "February", 0 },
                { "March", 0 },
                { "April", 0 },
                { "May", 0 },
                { "June", 0 },
                { "July", 0 },
                { "August", 0 },
                { "September", 0 },
                { "October", 0 },
                { "November", 0 },
                { "December", 0 },
                { "Total", 0 }
            },
        (acc, costRow) => 
            {
                acc["January"] += costRow.January;
                acc["February"] += costRow.February;
                acc["March"] += costRow.March;
                acc["April"] += costRow.April;
                acc["May"] += costRow.May;
                acc["June"] += costRow.June;
                acc["July"] += costRow.July;
                acc["August"] += costRow.August;
                acc["September"] += costRow.September;
                acc["October"] += costRow.October;
                acc["November"] += costRow.November;
                acc["December"] += costRow.December;
                acc["Total"] += costRow.Total;
                return acc;
            });

这将产生一个字典,其中键是一年中的几个月(和'总'),值是每个月的值的总和。

抱歉,我错过了VB.NET标签。 VB.NET不是我的强项,但我认为这与VB.NET相同。

Dim MyTotals = _
    AllTargetRows _
    .Aggregate( _
        New Dictionary(Of String, Integer) From { _
            {"January", 0}, _
            {"February", 0}, _
            {"March", 0}, _
            {"April", 0}, _
            {"May", 0}, _
            {"June", 0}, _
            {"July", 0}, _
            {"August", 0}, _
            {"September", 0}, _
            {"October", 0}, _
            {"November", 0}, _
            {"December", 0}, _
            {"Total", 0} _
        }, _
        Function(acc, costRow) 
            acc("January") += costRow.January
            acc("February") += costRow.February
            acc("March") += costRow.March
            acc("April") += costRow.April
            acc("May") += costRow.May
            acc("June") += costRow.June
            acc("July") += costRow.July
            acc("August") += costRow.August
            acc("September") += costRow.September
            acc("October") += costRow.October
            acc("November") += costRow.November
            acc("December") += costRow.December
            acc("Total") += costRow.Total
            Return acc
        End Function)

我不知道Aggregate函数的这个重载是否可以转换为查询语法。一个(可能更简单)的替代方案是完全放弃LINQ并在运行查询之前简单地创建字典,然后遍历每一行并以这种方式填充字典。像这样:

Dim MyTotals = _
    New Dictionary(Of String, Integer) From { _
        {"January", 0}, _
        {"February", 0}, _
        {"March", 0}, _
        {"April", 0}, _
        {"May", 0}, _
        {"June", 0}, _
        {"July", 0}, _
        {"August", 0}, _
        {"September", 0}, _
        {"October", 0}, _
        {"November", 0}, _
        {"December", 0}, _
        {"Total", 0}}

For Each costRow as AllTargetRow in AllTargetRows
    MyTotals("January") += costRow.January
    MyTotals("February") += costRow.February
    MyTotals("March") += costRow.March
    MyTotals("April") += costRow.April
    MyTotals("May") += costRow.May
    MyTotals("June") += costRow.June
    MyTotals("July") += costRow.July
    MyTotals("August") += costRow.August
    MyTotals("September") += costRow.September
    MyTotals("October") += costRow.October
    MyTotals("November") += costRow.November
    MyTotals("December") += costRow.December
    MyTotals("Total") += costRow.Total
Next