C# Aggregate() 方法


Aggregate() 方法对序列应用累加函数。

以下为我们的数组 −

string[] arr = { "DemoOne", "DemoTwo", "DemoThree", "DemoFour"};

现在使用 Aggregate() 方法。将 seed 值设置为 “DemoFive” 以进行比较。

string res = arr.AsQueryable().Aggregate("DemoFive", (longest, next) => next.Length > longest.Length ? next : longest,str => str.ToLower());

此处,结果字符串应具有比初始 seed 值(即 “DemoFive”)更多的字符数。

范例

 实时演示

using System;
using System.Linq;
class Demo {
   static void Main() {
      string[] arr = { "DemoOne", "DemoTwo", "DemoThree", "DemoFour"};
      string res = arr.AsQueryable().Aggregate("DemoFive", (longest, next) => next.Length >       longest.Length ? next : longest,str => str.ToLower());
      Console.WriteLine("The string with more number of characters: {0}", res);
   }
}

输出

The string with more number of characters: demothree

更新于: 22-6-2020

478 次浏览

启动您的 职业

完成课程认证

立即开始
广告