From the course: Complete Guide to .NET LINQ: Querying Collections, Databases, and Markup

Unlock this course with a free trial

Join today to access over 24,900 courses taught by industry experts.

Reverse a sequence with a custom aggregate

Reverse a sequence with a custom aggregate

- [Instructor] The Aggregate method isn't limited to numbers. It can be used on strings and other types as well. For example, let's say we have a list of food items shown here on line five and six. And I want to concatenate them in reverse order into a comma separated string. There are other ways to achieve this, but using Aggregate provides a functional approach. So I start with this list. It has the words, apple, banana, cherry. They're an alphabetical order, and then I'm defining an aggregate with a lambda. Remember, this first parameter represents the accumulated results, and this second parameter represents one of the items in the list or the sequence. So this will start with the string, a, apple, and b, banana. That's what's represented here, and what I'm doing is I'm grading an interpolated string, and I'm saying take the word banana and put it in the string at this position, then a comma and a space, and then take apple and put it in the second position. Then in this next…

Contents