LINQ OrderBY

By Riley K

LINQ offers a greate way to minimize your code. Here I will describe a small example of OrderBy using LINQ to sort a DropDownList.

Normally the traditional and most practice way of sorting a dropdownlist is using ArrayList that I have described in below link

http://www.eggheadcafe.com/sample-code/ASP.NET/226deb7d-7e31-49bb-a074-6593a4c0681f/sorting-a-dropdownlist-using-arraylist.aspx

Using LINQ OrderBY it is very easy using a single Method

var dc = new DataClassesDataContext();

var bindDDL = (from cnt in dc.Countries
                      select new { CountryName= cnt.CountryName, ID=cnt.ID }).OrderBy(o=>o.CountryName);

DropDownList1.DataSource = bindDDL;
DropDownList1.DataTextField = "CountryName";
DropDownList1.DataValueField = "ID";
DropDownList1.DataBind();

LINQ OrderBY  (1548 Views)