Blog

Aggregate function in lambda C#

Lambda

Aggregate function in lambda C#

  1. Create a console application and create a customer class in it.
				
					public class Customer
    {
        public int CustomerCode { get; set; }
        public string CustomerName { get; set; }
        public int TotalOrder { get; set; }
    }
				
			

2. Create a list of customers.

				
					var customerList = new List<Customer>()
    {
        new Customer{CustomerCode=101,CustomerName = "Steve", TotalOrder = 2},
        new Customer{CustomerCode=102,CustomerName = "Mark", TotalOrder = 4},
        new Customer{CustomerCode=103,CustomerName = "Bill", TotalOrder = 7},
        new Customer{CustomerCode=104,CustomerName = "Dell",TotalOrder = 6}
    };
				
			

3. Now with the help of the lambda expression we will get the summation of the total orders from the customer list.

				
					var sum = customerList.Sum(a => a.TotalOrder);
				
			

Please comment about how we can enhance our blog posts and please share the post so that dotnercoaching can be more accessible and you become a part of our mission to spread free knowledge to make a better future.

Subhajit