代码之家  ›  专栏  ›  技术社区  ›  zs2020

如何像python中的reduce()方法一样,使用C中的Linq将函数应用于列表中的每个元素?

  •  3
  • zs2020  · 技术社区  · 15 年前

    2 回复  |  直到 15 年前
        1
  •  11
  •   Cᴏʀʏ bcherry    15 年前

    假设你说的是 this reduce function ,在C#和LINQ中的等价物是 Enumerable.Aggregate .

    var list = Enumerable.Range(5, 3); // [5, 6, 7]
    Console.WriteLine("Aggregation: {0}", list.Aggregate((a, b) => (a + b)));
    // Result is "Aggregation: 18"
    
        2
  •  2
  •   KeithS    15 年前