LINQ to XML where condition between to filter records with order by specific column

In this example we will apply where condition to Salary column and get the employee details who are having salary between 20,000 and 50,000 and order by Salary ascending.

Example:

Design:


Code Behind (C#):

System.Xml.Linq.XDocument xdoc = System.Xml.Linq.XDocument.Parse(@" Thomas Executive Accounts 5000 Wills Manager Accounts 24000 Brod Manager Finance 28000 Smith Analyst Finance 21000 "); var res = from emp in xdoc.Root.Elements() where 20000 < (decimal)emp.Element("Salary") && (decimal)emp.Element("Salary") < 25000 orderby (decimal)emp.Element("Salary") ascending select new { EmployeeName = emp.Element("Name").Value, Designation = emp.Element("Designation").Value, Department = emp.Element("Department").Value, Salary = emp.Element("Salary").Value }; gvXML.DataSource = res; gvXML.DataBind();

Output:

EmployeeName

Designation

Department

Salary

Smith

Analyst

Finance

21000

Wills

Manager

Accounts

24000