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 次瀏覽

啟動您的 職業

完成課程認證

立即開始
廣告