Objective

In this article, I am going to show, how we could achieve filtering and sorting in LINQ to object

I have created two classes for my explanation purpose. Student class is having details of students and Hostel class is having details of hostel. Both classes are having a properties HostelNumber common. I will be using this property to perform join operations in later articles. Both classes are as below.

Student.cs

namespace LINQtoOBJECT1

{

public class Student

{
public int RollNumber { get; set; }
public string Name { get; set; }
public int Section { get; set; }
public int HostelNumber { get; set; }
}
}

 Hostel.cs

namespace LINQtoOBJECT1

{

public class Hostel
{
public int HostelNumber { get; set; }
public int NumberofRooms { get; set; }
}}

To create collection of students and hostels, I am creating two static functions.

 Function to return collection of students.

static List<Student> GetStudents(){

List<Student> students = new List<Student>{

new Student() { RollNumber = 1,Name =“Alex “ , Section = 1 ,HostelNumber=1 },

new Student() { RollNumber = 2,Name =“Jonty “ , Section = 2 ,HostelNumber=2 },
new Student() { RollNumber = 3,Name =“Samba “ , Section = 3 ,HostelNumber=1 },
new Student() { RollNumber = 4,Name =“Donald “ , Section = 3 ,HostelNumber=2 },
new Student() { RollNumber = 5,Name =“Kristen “ , Section = 2 ,HostelNumber=1 },
new Student() { RollNumber = 6,Name =“Mark “ , Section = 1 ,HostelNumber=2},
new Student() { RollNumber = 7,Name =“Gibbs “ , Section = 1 ,HostelNumber=1 },
new Student() { RollNumber = 8,Name =“Peterson “ , Section = 2 ,HostelNumber=2 },
new Student() { RollNumber = 9,Name =“collingwood “ , Section = 3 ,HostelNumber=1 },
new Student() { RollNumber = 10,Name =“Brian “ , Section = 3 ,HostelNumber=2 }
};
return students;
}

Function to return collection of hostels

static List<Hostel> GetHostel(){

List<Hostel> hostels = new List<Hostel>
{
new Hostel(){HostelNumber=1 ,NumberofRooms = 100},
new Hostel(){HostelNumber= 2 ,NumberofRooms = 200}
};
return hostels;
}

 Now have a look on both codes below, one is using LINQ and other is using LOOP to retrieve data from the list and print.
Note: I will be using above two classes and functions for my entire sample below.
Filtering in LINQ


 In below code, I am filtering result. I am only fetching detail of student with RollNumber 1.


List<Student> lstStudents = GetStudents();

Student student = (from r in lstStudents where r.RollNumber == 1 select r).First();

Console.WriteLine(student.Name + student.RollNumber + student.HostelNumber + student.Section);

 If you see closely in above LINQ query, I am using First clause. Because if I am returning the output in a Student type and default return type of LINQ is IEnumerable. If I modify the above LINQ as below

 Student student = (from r in lstStudents where r.RollNumber == 1 select r);

 At compile time we can expect below error,


When we run the above code

Output

Now if I want to have, named of all the students whose roll number is more than 3 and who resides in hostel number 2.

List<Student> lstStudents = GetStudents();

IEnumerable<string> lstStudentName = from r in lstStudents where r.RollNumber > 3 && r.HostelNumber==2
select r.Name ;

foreach (string name in lstStudentName)
Console.WriteLine(name);

In above LINQ query, I am applying both filtering and projection.
Output


 Intermediate values
If I want to have intermediate values in my LINQ query then LET clause come into action. LET clause helps to reduce redundancy in WHERE clause.

IEnumerable<string> lstStudentName = from r in lstStudents let condition = r.RollNumber where condition > 3 && r.HostelNumber== 2
select r.Name ;

Let clause can be having complex statements.
Sorting in LINQ
OrderBy clause is used for purpose of sorting in LINQ.
Sorting a single property
Just before WHERE clause, OrederBy clause is used to sort the query result. In code Name of the students will be displayed in ascending order.

List<Student> lstStudents = GetStudents();
IEnumerable<string> lstStudentName = from r in lstStudents let condition = r.RollNumber orderby r.Name where condition > 3 && r.HostelNumber== 2
select r.Name ;

foreach (string name in lstStudentName)

Console.WriteLine(name);

 Output


 Sorting multiple properties
By putting comma between properties multiple properties can be sorted. Sorting will be done left to right. For roll number property I am sorting in descending order. This can be achieved by using keyword DESCENDIG. By default sorting is ascending.


List<Student> lstStudents = GetStudents();

IEnumerable<Student> lstStudentName = from r in lstStudents let condition = r.RollNumber orderby r.Name,r.RollNumber descending
where condition > 3 && r.HostelNumber== 2
select r ;

foreach (Student name in lstStudentName)

Console.WriteLine(name.RollNumber + name.Name);


 Conclusion
In next part, I will be explaining Grouping. I hope this article was useful to you. Thanks for reading.

Advertisements

2 thoughts on “LINQ to Object Part #2: Filtering and Sorting

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s