.NET Tutorials, Forums, Interview Questions And Answers
Welcome :Guest
 
Sign In
Register
 
Win Surprise Gifts!!!
Congratulations!!!


Top 5 Contributors of the Month
david stephan

Home >> Code Snippets >> LINQ >> Post New Resource Bookmark and Share   

 Subscribe to Code Snippets

Sorting And Filtering

Posted By:Parvathi       Posted Date: June 17, 2010    Points: 10    Category: LINQ    URL: http://www.dotnetspark.com  

Created a list of students. Sort the list using LINQ order by clause and filtering the data using where clause
 

Aspx page & Code behind

 
        
            

Sorting

Note OrderBy clause is used for purpose of sorting in LINQ.

Sorting a single property - Sort By Name & Roll Numer

List lstStudents = GetStudents();

 static List GetStudents()
        {
            List students = new List
              {
                new Student() { RollNumber = 1,Name ="Minu " , Section = 1 ,HostelNumber=1 },
                new Student() { RollNumber = 2,Name ="Bimal " , Section = 2 ,HostelNumber=2 },
                new Student() { RollNumber = 3,Name ="Parvathi " , Section = 3 ,HostelNumber=1 },
                new Student() { RollNumber = 4,Name ="Sanju" , Section = 3 ,HostelNumber=2 },
                new Student() { RollNumber = 5,Name ="Piyush " , Section = 2 ,HostelNumber=1 },
                new Student() { RollNumber = 6,Name ="Unni " , Section = 1 ,HostelNumber=2},
                new Student() { RollNumber = 7,Name ="Roshni " , Section = 1 ,HostelNumber=1 },
                new Student() { RollNumber = 8,Name ="Jiss " , Section = 2 ,HostelNumber=2 },
                new Student() { RollNumber = 9,Name ="Sudeepthy " , Section = 3 ,HostelNumber=1 },
                new Student() { RollNumber = 10,Name ="Tony " , Section = 3 ,HostelNumber=2 }
               };
            return students;
        }


protected void btnsort_Click(object sender, EventArgs e)
        {
            List lstStudents = GetStudents();
            GridView1.DataSource = from r in lstStudents
                                   let condition = r.RollNumber
                                   orderby r.Name
                                   where condition > 3 && r.HostelNumber == 2
                                   select new
                                   {
                                       RollNumber = r.RollNumber,
                                       Name = r.Name
                                   };

            GridView1.DataBind();
        }

     

Further Readings:

Responses

No response found. Be the first to respond this post

Post Comment

You must Sign In To post reply
Find More code samples in C#, ASP.Net, Vb.Net and more Here

Hall of Fame    Twitter   Terms of Service    Privacy Policy    Contact Us    Archives   Tell A Friend