LINQ sum项目集合返回带有结果的对象(多列)

时间:2011-12-12 12:57:21

标签: c# linq

我知道我可以用foreach做下面的事情,但是想知道是否有一种干净且“性感”的方式用LINQ做到这一点。

public class item
{
    public int total { get; set; }
    public int net { get; set; }
}


class Program
{
    static void Main(string[] args)
    {
        List<item> items = new List<item>()
                               {
                                   new item() { total = 123, net = 423},
                                   new item() { total = 432, net = 54},
                                   new item() { total = 33, net = 57654},
                                   new item() { total = 33, net = 423},
                                   new item() { total = 3344, net = 423},
                                   new item() { total = 123, net = 423},
                                   new item() { total = 123, net = 98},
                                   new item() { total = 123, net = 867},
                                   new item() { total = 123, net = 876},
                                   new item() { total = 123, net = 423},
                                   new item() { total = 123, net = 543},
                                   new item() { total = 543, net = 345},
                               };

        item i = new item();
        foreach (var item in items)
        {
            i.net += item.net;
            i.total += item.total;
        }
    }
}

我想要做的是,对于给定的对象列表,对每个列/字段求和并返回一个具有每个值之和的单个对象。

我试过了:

var result = (from e in items
                     select new
                                {
                                    NET_GRAND = e.net,
                                    TOTAL_GRAND = e.total
                                }).ToList();

以下的变化,但没有运气:

 var result = (from t in items
                     group t by new {t.net, t.total}
                     into grp
                     select new
                                {
                                    NET_GRAND = grp.Sum(t => t.net),
                                    TOTAL_GRAND = grp.Sum(t => t.total)
                                }).GroupBy(x => new { x.NET_GRAND, x.TOTAL_GRAND }).ToList();

修改

应该指出效率在这里以及性感都很重要。

6 个答案:

答案 0 :(得分:16)

如果您不关心迭代列表两次,

var i = new item
    { 
        net = items.Sum(it => it.net), 
        total = items.Sum(it => it.total) 
    };

如果你关心迭代列表两次(就像你为未知来源的IEnumerable做的那样),

var i = items.Aggregate(new item(), 
    (accumulator, it) => 
        new item 
        {
            net = accumulator.net + it.net, 
            total = accumulator.total + it.total 
        } 
);

答案 1 :(得分:11)

看起来你真的想要:

var result = new {
    NetGrand = items.Sum(t => t.net),
    TotalGrand = items.Sum(t => t.total)
};

另一方面,我可能只是将它们分成两个不同的局部变量:

var netGrand = items.Sum(t => t.net);
var totalGrand = items.Sum(t => t.total);

当然,这会在列表上重复两次,但在大多数情况下,我希望不会引起注意。

答案 2 :(得分:2)

item totals = new item 
              { 
                 net = items.Sum(i => i.net),
                 total = items.Sum(i => i.total)  
              };

但请记住,此查询将枚举列表两次,因此对于大型列表,这将不如旧的单一foreach循环那样有效。

答案 3 :(得分:1)

var item = new item();
item.net = items .Sum(x=>x.net);
item.total = items.Sum(x=>x.total);

答案 4 :(得分:0)

使用foreach循环。你声明你关心效率,即使你不是没有理由用Linq写它只是为了使用Linq。

我们发现,当我们获得更多程序经验时,我们发现的一件事就是因为某些事情已经完成,“旧方法”并没有让它变得糟糕。转换为新的wiz-bang方法并没有让它变得更好。事实上,如果您的代码以旧的方式工作,“升级”是注入缺陷的原因,在许多情况下没有优势。

充其量,这个Linq方法需要花费2倍的时间来计算。

var i = new item
    { 
        net = items.Sum(it => it.net), 
        total = items.Sum(it => it.total) 
    };

不确定聚合方法,但显然需要更长时间。

答案 5 :(得分:0)

我相信您可以使用 LINQ 完成此操作,而无需通过使用 GroupBy 和键的常量值(在本例中我使用 1)进行迭代。

item totals = items.GroupBy(i => 1).Select(g => new item()
{
    net = g.Sum(i => i.net),
    total = g.Sum(i => i.total)
}).Single();