Now let's first create a console application and add class file in it. In this we will add the following class property.
public class EmployeeModel
{
public int Id { get; set; }
public string Name { get; set; }
public string Address { get; set; }
public string Email { get; set; }
}
In above code I have declared some properties of different data type like int, string. We will make list of this properties. Select() In LINQ
Select In LINQ is a method where we will select column values from a list. Here we will use Select () methos and with the lambda expression we will select the columns of the list for which we want to get the value.
Single Column
List<EmployeeModel> employees = new List<EmployeeModel>() {
new EmployeeModel { Id=1,Name="John" ,Address="Address10",Email="john@mail.com"},
new EmployeeModel { Id=2,Name="Mate" ,Address="Address27",Email="mate@mail.com"},
new EmployeeModel { Id=4,Name="Maru" ,Address="Address10",Email="maru@mail.com"},
new EmployeeModel { Id=3,Name="Rose" ,Address="Address31",Email="rose@mail.com"},
new EmployeeModel { Id=5,Name="Krishna" ,Address="Address33",Email="krishna@mail.com"},
new EmployeeModel { Id=8,Name="Pape" ,Address="Address24",Email="pape@mail.com"},
new EmployeeModel { Id=10,Name="Pranav" ,Address="Address77",Email="pranav@mail.com"}
};
var data = employees.Select(m => m.Name).ToList();
foreach (var item in data)
{
Console.WriteLine(string.Format("Name:{0} ", item));
}
In above code I have added some value in the employee List. To select a specific field from the list, Select() methos is used in c#.net. Now let's run the code to check the output.Multiple Column
In this code example i will show you how you can select multiple collum from a list using Select() method.
List<EmployeeModel> employees = new List<EmployeeModel>() {
new EmployeeModel { Id=1,Name="John" ,Address="Address10",Email="john@mail.com"},
new EmployeeModel { Id=2,Name="Mate" ,Address="Address27",Email="mate@mail.com"},
new EmployeeModel { Id=4,Name="Maru" ,Address="Address10",Email="maru@mail.com"},
new EmployeeModel { Id=3,Name="Rose" ,Address="Address31",Email="rose@mail.com"},
new EmployeeModel { Id=5,Name="Krishna" ,Address="Address33",Email="krishna@mail.com"},
new EmployeeModel { Id=8,Name="Pape" ,Address="Address24",Email="pape@mail.com"},
new EmployeeModel { Id=10,Name="Pranav" ,Address="Address77",Email="pranav@mail.com"}
};
var data = employees.Select(m => new { m.Name, m.Email }).ToList();
foreach (var item in data)
{
Console.WriteLine(string.Format("Name:{0}, Email Id:{1}", item.Name,item.Email));
}
In above code just check the highlighted piece of code. Here I have used new keyword and get the column name with lambada expression. Now let's run the code and check the output. List<EmployeeModel> employees = new List<EmployeeModel>() {
new EmployeeModel { Id=1,Task=new List<string>(){"Task 1","Task 2","Task 3" },Name="John" },
new EmployeeModel { Id=2,Task=new List<string>(){"Task 1","Task 2" },Name="Mate" ,},
new EmployeeModel { Id=4,Task=new List<string>(){"Task 3" },Name="Maru" },
new EmployeeModel { Id=3,Task=new List<string>(){"Task 1","Task 2","Task 3" }},
new EmployeeModel { Id=5,Task=new List<string>(){"Task 2" },Name="Krishna"},
new EmployeeModel { Id=8,Task=new List<string>(){"Task 2","Task 3" },Name="Pape"},
new EmployeeModel { Id=10,Task=new List<string>(){"Task 1","Task 3" },Name="Pranav" }
};
var data = employees.SelectMany(m => m.Task).ToList();
foreach (var item in data)
{
Console.WriteLine(string.Format("Task:{0}", item));
}
In above code you can see that in Employee List there is a list named as Task. Now we will use SelectMany() to get the list of all tasks. To get the deistic value we can use Distinct() method to remove the duplicate values. Now we have done run the code to check the output.