You are reading the article How Aggregate Works In Linq With Examples? updated in October 2023 on the website Vibergotobrazil.com. We hope that the information we have shared is helpful to you. If you find the content interesting and meaningful, please share it with your friends and continue to follow and support us for the latest updates. Suggested November 2023 How Aggregate Works In Linq With Examples?
Introduction to LINQ AggregateLINQ Aggregate method can create its own custom aggregation operation on values of sequence or collections of items. Aggregate function requires assembling the values together of several rows as input and it returns a single value as a result. LINQ Aggregate function execution starts from the first value and second value and then it carries forward the result for the further operation it takes the last result as the first value and third value and then it carries forward and so on. Aggregate methods we can derive from the namespace System. Linq and the method available in both the classes Queryable and Enumerable.
Start Your Free Software Development Course
Web development, programming languages, Software testing & others
LINQ Aggregate methods have three overloaded methods, it performs an accumulation operation. Let’s see the following syntax of aggregate extension method,
How Aggregate works in LINQ?LINQ Aggregate functions result in a single value. LINQ Aggregate function execution starts from the first value and second value and then it carries forward the result for the further operation it takes the last result as the first value and third value and then it carries forward and so on. The aggregate method only supports Method Syntax and it does not support query syntax. Aggregate method available in both classes Queryable and Enumerable. It is an extension method from the namespace System.Linq. Let’s see the following example which describes the working flow of aggregate function,
Code:
using System; using System.Collections.Generic; using System.Linq; using System.Text; using System.Threading.Tasks; namespace Console_LinqAggregate { class Program { static void Main(string[] args) { string[] Skill_Set = {"MVC","EntityFramework","Sqlserver","Asp.net","Linq", "C#.net", "Web-Api","Jquery"}; Console.WriteLine("nLINQ Aggregate - Using Seperator-Comma : nn" + Seperated_Result); Console.ReadLine(); } } }This program it uses the function Aggregate((string_1, string_2), this method applies the accumulator function in the collection. The particular seed value is the initial accumulator value and that particular function selects the resultant value.
Initially it contains a null value as shown below,
Here we concatenating the items of Skill_Set in a separated string as follows,
Finally, it concatenates all the strings with a comma separator,
Output:
ExamplesAggregate function requires assembling the values together of several rows as input and it returns a single value as a result. Aggregate function is an extension method from the namespace System. Linq. Let’s see the following example programmatically,
Example #1Code:
In this program, it shows the Aggregate Method – Seed Value the method used to add the user’s age in the collection of items. In the second overloaded function the aggregate method takes the first argument as the seed value to accumulate and the second argument as the Function type delegate, let’s see the example programmatically which use the seed parameter as shown below,
using System; using System.Collections.Generic; using System.Linq; using System.Text; using System.Threading.Tasks; namespace Console_LinqAggregate { class Program { public class UserClass { public string userName { get; set; } public int userAge { get; set; } static void Main(string[] args) { { new UserClass { userName = “Smith”, userAge = 23 }, new UserClass { userName = “Rio”, userAge = 30}, new UserClass { userName = “Jack”, userAge = 41}, new UserClass { userName = “Danial”, userAge = 25 }, new UserClass { userName = “Peter”, userAge = 32 }, }; Console.WriteLine(“nAggregate Method – Seed Value”); Console.WriteLine(“nSum of Users- Age in the List:”); Console.WriteLine(“n”+get_SumOf_UserAge); Console.ReadLine(); } } } }
In this above program, the aggregate method is used to add the user’s age in the collection of items. In the second overloaded function the aggregate method takes the first argument as the seed value to accumulate and the second argument as the Function type delegate, let’s see the example programmatically which use the seed parameter as shown below,
Example #2Code:
using System; using System.Collections.Generic; using System.Linq; using System.Text; using System.Threading.Tasks; namespace Console_LinqAggregate { class Program { public class UserClass { public string userName { get; set; } public int userAge { get; set; } static void Main(string[] args) { { new UserClass { userName = “Smith”, userAge = 23 }, new UserClass { userName = “Rio”, userAge = 30}, new UserClass { userName = “Jack”, userAge = 41}, new UserClass { userName = “Danial”, userAge = 25 }, new UserClass { userName = “Peter”, userAge = 32 }, }; String.Empty, Console.WriteLine(“nAggregate Method with Result Selector”); Console.WriteLine(“n” + seperated_UserName); Console.ReadLine(); } } } }
In the above program, we described the lambda expression as str.Substring(0, str. Length – 1)); it eliminate the last appeared comma in the resultant string. Let’s see the following output below,
Output:
ConclusionIn LINQ Aggregate() method there are various methods to target our program, which helps to create our own code in a customizable way. Hope the article is helpful to understand the necessity and usage of Aggregate Method by seeing the above examples programmatically.
Recommended ArticlesThis is a guide to LINQ Aggregate. Here we discuss the Introduction, syntax, How aggregate works in LINQ? examples with code implementation. You may also have a look at the following articles to learn more –
You're reading How Aggregate Works In Linq With Examples?
Update the detailed information about How Aggregate Works In Linq With Examples? on the Vibergotobrazil.com website. We hope the article's content will meet your needs, and we will regularly update the information to provide you with the fastest and most accurate information. Have a great day!