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
广告