Substring in lambda C#
August 9, 2022 2022-08-13 3:42Substring in lambda C#
- 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()
{
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 substring in the lambda expression we will be able to get the 1st two letters for each customers.
var result = customerList.Select(a => a.CustomerName.Substring(0,2)).ToList();
foreach (var a in result)
{
Console.WriteLine(a.ToString());
}
Console.ReadLine();

Please comment and share if you like this post and tell us about how we can enhance our posts. Thanks.
Subhajit
This is Subhajit Sinha, with more than 8 years of experience as a full stack developer in renowned Multinational Companies, here in this portal as an online tutor I will guide you personally to level up your programming skills and also will coach you to augment your career and salary as well.
Search
Popular posts
