C# Aggregate() 方法

programmingserver side programmingcsharp更新于 2025/9/15 23:12:00

Aggregate() 方法将累加器函数应用于序列。

以下是我们的数组 −

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

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

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

此处,结果字符串的字符数应大于初始种子值,即"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

相关文章