- Data Structure
- Networking
- RDBMS
- Operating System
- Java
- MS Excel
- iOS
- HTML
- CSS
- Android
- Python
- C Programming
- C++
- C#
- MongoDB
- MySQL
- Javascript
- PHP
- Physics
- Chemistry
- Biology
- Mathematics
- English
- Economics
- Psychology
- Social Studies
- Fashion Studies
- Legal Studies
- Selected Reading
- UPSC IAS Exams Notes
- Developer's Best Practices
- Questions and Answers
- Effective Resume Writing
- HR Interview Questions
- Computer Glossary
- Who is Who
C# Aggregate() method
The Aggregate() method applies an accumulator function over a sequence.
The following is our array −
string[] arr = { "DemoOne", "DemoTwo", "DemoThree", "DemoFour"};
Now use the Aggregate() method. We have set the ssed value as “DemoFive” for comparison.
string res = arr.AsQueryable().Aggregate("DemoFive", (longest, next) => next.Length > longest.Length ? next : longest,str => str.ToLower());
Here, the resultant string should have more number of characters than the initial seed value i.e. “DemoFive”.
Example
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); } }
Output
The string with more number of characters: demothree
Advertisements
To Continue Learning Please Login
Login with Google