Blog

Select a specific record in lambda C#

Lambda

Select a specific record 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"},
                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 a specific record from the customer list where customer code = 102

				
					var result = customerList.Find(a => a.CustomerCode == 102);
				
			

3. Now we will print the result in the console

				
					Console.WriteLine(result.CustomerCode);
Console.ReadLine();
				
			

The result will be as follows

dotnetcoaching

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