order by Clause in LINQ C#



OrderBy sorts the values of a collection in ascending or descending order.
It sorts the collection in ascending order by default because the ascending keyword is optional here.
Use descending keywords to sort collections in descending order.

using System;
using System.Linq;

namespace CSharpLINQ
{
    class SimpleQuery
    {
		static void Main( )
		{
			var students = new[] // Array of objects of an anonymous type
			{
				new { LName="Mishra", FName="Kuldeep", Age=31 },
				new { LName="Sharma", FName="Ramesh", Age=20 },
				new { LName="Verma", FName="Manoj", Age=21 },
				new { LName="Mishra", FName="Rajesh", Age=45 },
				new { LName="Soni", FName="Mukesh", Age=35 },
				new { LName="Goyal", FName="Suresh", Age=15}
			};

			var query = from student in students orderby student.Age descending select student;
			
			foreach (var q in query)
				Console.WriteLine("Name: {0},  Age: {1}", q.FName, q.Age);
		}
    }
}

0 Comment's

Comment Form