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

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";

LINQ OrderBY  (2814 Views)