how to use aggregate operators in linq

An aggregate function works on more than one element. Examples of aggregate operators are MAX, SUM and Average. The below code example demonstrates how to use aggregate operators Average, Count and Sum.

How to use Aggregate function Sum with LINQ

The below code creates a list which contains product names, category and sale amount.
List products = new List{new Product { 
                                 Name = "Product1",
                                 Category ="Vegetable" , 
                                 SaleAmount=100  },
                                 new Product { 
                                 	Name = "Product2",
                                 	Category ="Bakery product" , 
                                 SaleAmount=200  },
                                  new Product{ 
                                    Name = "Product3",
                                    Category ="Vegetable" , 
                                    SaleAmount=300 },
                                 new Product { 
                                    Name = "Product4",
                                    Category ="Bakery product" , 
                                 	SaleAmount=400  },
                                 new Product{ 
                                     Name = "Product5",
                                     Category ="Drink" , 
                                     SaleAmount=500 },
                                 new Product { 
                                     Name = "Product6",
                                     Category ="Drink" , 
                                     SaleAmount=600  },
            };
           var query2 = from amt in products
                         group amt by amt.Category into result
                         select new
                         {
                             Name = result.Key,
                             Sum = result.Sum(i => i.SaleAmount)
                         };
           foreach (var q in query2)
           { Console.WriteLine(string.Format("Name: {0} ,
            Sum sales: {1}", q.Name, q.Sum)); }
            Console.ReadLine();

The above query groups products based on category and adds the sale amount using Sum aggregate operator. The lambda expression is used with Sum operator.

How to use Aggregate function Count
The dictionary products stores the names of the products and its category. The query groups the products based on the category. We use the Count aggregate operator to count the number of products under each category.

Dictionary products;
products = new Dictionary();
products.Add("Product1", "Vegetables");
products.Add("Product2", "Vegetables");
products.Add("Product3", "Non Sodium Items");
products.Add("Product4", "Non Sodium Items");
products.Add("Product5", "Bakery Products");
var productGroups =
				from product in products
				group product by product.Value
				into productGroup
				select productGroup;
foreach (var group in productGroups)
{
   Console.WriteLine("We discovered {0} {1}", group.Count(), group.Key);
   foreach (var product in group)
     {
          Console.WriteLine(product.ToString() + " ");
     }
}

How to use Aggregate function Average

var query2 =from amt in products
			group amt by amt.Category into result
			select new
			{
				Name = result.Key,
				Average = result.Average(i => i.SaleAmount)
			};
foreach (var q in query2)
{ 
	Console.WriteLine(string.Format("Name: {0} , 
    Sum sales: {1}", q.Name, q.Average)); 
}
Console.ReadLine();


The Average aggregate operator in the above query calculates the average value.