In this article we are going to discuss on SELECT extension method, the most commonly used method in LINQ.
You can understand about SELECT method while we will walk through this post.
In order to work with all the examples below, I am going to first create a Class called Student, which will looks like this
public class Student
{
public int StudID { get; set; }
public string Name { get; set; }
public string Class { get; set; }
public List Subjects { get; set; }
}
Then I am going to populate some data into the student with List<>
//Create some students
List students = new List()
{
new Student { StudID = 101, Name= "Karthik", Class = "10th", Subjects = new List { "C#", "ADO.Net", "EF"}},
new Student { StudID = 108, Name= "Karthik", Class = "10th", Subjects = new List { "C#", "ADO.Net", "EF"}},
new Student { StudID = 104, Name = "Ram", Class = "11th", Subjects= new List{ ".Net", "C#", "SQL", "EF"}},
new Student { StudID = 103, Name = "John", Class = "9th", Subjects= new List{ ".Net", "MVC"}},
new Student { StudID = 106, Name = "Bill", Class = "9th", Subjects= new List{ "EF", "MVC"}},
};
Well, now we are ready to see all the examples for this topic.
Example 1
Select
all the students and show their name and classes
//Select All the students with their Name and Classes
//USING QUERY SYNTAX
Console.WriteLine("WITH QUERY SYNTAX\n");
var result = from student in students
select student;
foreach (var item in result)
{
Console.WriteLine(item.Name + "\t" + item.Class);
}
Console.WriteLine("\nWITH METHOD SYNTAX\n");
//USING METHOD SYNTAX
var resultM = students.Select(x => x);
foreach (var item in resultM)
{
Console.WriteLine(item.Name + "\t" + item.Class);
}
As you can see above, I have just used select method to select all the students and then I stored all the output in a
var type which is nothing but
Implicitly typed variable , hence it can store any type of data. I am then iterating through all the elements and getting the result back.
Example 2
Select
Unique record from the list of data
//Select UNIQUE NAMES from students (remove duplicate)
Console.WriteLine("\nUNIQUE DATA -- Using UNIQUE method\n");
var Dresult = (from student in students
select new { student.Name, student.Class }).Distinct();
foreach (var item in Dresult)
{
Console.WriteLine(item.Name + "\t" + item.Class);
}
As you can see above, I have used a
select new { … } which is nothing but using
Anonymous Type , all I am doing is
projecting the select data to a new anonymous type (just the name and class) and then I am using
Distinct extension method to get the distinct value from the list of data.
Example 3
Getting
First and
Last Record from the List
//To select the first value from List of students
Console.WriteLine("\nFIRST Record from List -- Using FIRST method");
var first = students.First();
Console.WriteLine(first.Name + "\t" + first.Class);
//To select the last value from List of students
Console.WriteLine("\nLAST Record from List -- Using Last method");
var last = students.Last();
Console.WriteLine(last.Name + "\t" + last.Class);
Example 4
Select Student whose studID is
greater than 103
//Select students whose studentid is greater than 103
Console.WriteLine("\nStudent ID greater than 103");
var gStudents = from student in students
where student.StudID > 103
select student;
foreach (var item in gStudents)
{
Console.WriteLine(item.StudID + "\t" + item.Name + "\t" + item.Class);
}
As you can see above, I have just used an additional where clause (extension method) and using checked if the studID is greater than 103 and then selected the value. Its that straight forward.
Example 5
Just try to add
initials in all the names as “
.KK”
//Change all the Name by adding initial as ".KK"
Console.WriteLine("\nNames with added initials");
var initial = from student in students
select new { Name = student.Name + ".KK", StudentID = student.StudID };
foreach (var item in initial)
{
Console.WriteLine(item.StudentID + "\t" + item.Name);
}
Example 6
Class with th removed from the list
//Remove all the th from the Class
Console.WriteLine("\nClass with th removed");
var remove = from student in students
select new { Name = student.Name, Class = student.Class.Replace("th", "") };
foreach (var item in remove)
{
Console.WriteLine(item.Name + "\t" + item.Class);
}
Here is the complete code
public static void SelectLINQ()
{
//Create some students
List students = new List()
{
new Student { StudID = 101, Name= "Karthik", Class = "10th", Subjects = new List { "C#", "ADO.Net", "EF"}},
new Student { StudID = 108, Name= "Karthik", Class = "10th", Subjects = new List { "C#", "ADO.Net", "EF"}},
new Student { StudID = 104, Name = "Ram", Class = "11th", Subjects= new List{ ".Net", "C#", "SQL", "EF"}},
new Student { StudID = 103, Name = "John", Class = "9th", Subjects= new List{ ".Net", "MVC"}},
new Student { StudID = 106, Name = "Bill", Class = "9th", Subjects= new List{ "EF", "MVC"}},
};
//Select All the students with their Name and Classes
//USING QUERY SYNTAX
Console.WriteLine("WITH QUERY SYNTAX\n");
var result = from student in students
select student;
foreach (var item in result)
{
Console.WriteLine(item.Name + "\t" + item.Class);
}
Console.WriteLine("\nWITH METHOD SYNTAX\n");
//USING METHOD SYNTAX
var resultM = students.Select(x => x);
foreach (var item in resultM)
{
Console.WriteLine(item.Name + "\t" + item.Class);
}
//Select UNIQUE NAMES from students (remove duplicate)
Console.WriteLine("\nUNIQUE DATA -- Using UNIQUE method\n");
var Dresult = (from student in students
select new { student.Name, student.Class }).Distinct();
foreach (var item in Dresult)
{
Console.WriteLine(item.Name + "\t" + item.Class);
}
//To select the first value from List of students
Console.WriteLine("\nFIRST Record from List -- Using FIRST method");
var first = students.First();
Console.WriteLine(first.Name + "\t" + first.Class);
//To select the last value from List of students
Console.WriteLine("\nLAST Record from List -- Using Last method");
var last = students.Last();
Console.WriteLine(last.Name + "\t" + last.Class);
//Select students whose studentid is greater than 103
Console.WriteLine("\nStudent ID greater than 103");
var gStudents = from student in students
where student.StudID > 103
select student;
foreach (var item in gStudents)
{
Console.WriteLine(item.StudID + "\t" + item.Name + "\t" + item.Class);
}
//Change all the Name by adding initial as ".KK"
Console.WriteLine("\nNames with added initials");
var initial = from student in students
select new { Name = student.Name + ".KK", StudentID = student.StudID };
foreach (var item in initial)
{
Console.WriteLine(item.StudentID + "\t" + item.Name);
}
//Remove all the th from the Class
Console.WriteLine("\nClass with th removed");
var remove = from student in students
select new { Name = student.Name, Class = student.Class.Replace("th", "") };
foreach (var item in remove)
{
Console.WriteLine(item.Name + "\t" + item.Class);
}
}
Output
We can do still lot of stuffs in
LINQ, which we can discuss in my next article of LINQ series.
Please leave your feedback and comments !!!
Thank,
Karthik KK