Blog

Select query in lambda in C#

Lambda

Select query in lambda in 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 in it.

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

            };
				
			

3. Now with the help of Lambda expression, select the list of customer codes from the customerList

				
					var result = customerList.Select(a => a.CustomerCode).ToList();
				
			

3. Now in the “a” we will get all the codes in the customerList

4. Now we will loop through the customerList to get it printed

dotnetcoaching
				
					        var result = customerList.Select(a => a.CustomerCode).ToList();
            result.ForEach(a => Console.WriteLine(a));
            Console.ReadLine();
				
			

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