step1: Create a data table in C# , with some column and with their value
private static DataTable CreateDataTable()
{
DataTable dt = new DataTable
{
TableName = "dt1"
};
dt.Columns.Add("Id", typeof(int));
dt.Columns.Add("Name");
dt.Columns.Add("City");
dt.Columns.Add("Department");
dt.Columns.Add("Salary", typeof(int));
dt.Rows.Add(1, "N1", "c1", "d1", 100);
dt.Rows.Add(2, "N2", "c2", "d2", 100);
dt.Rows.Add(3, "N3", "c3", "d3", 150);
dt.Rows.Add(4, "N4", "c4", "d4", 120);
dt.Rows.Add(5, "N5", "c5", "d5", 480);
return dt;
}
get the result:
public static void DatatableTest()
{
var dt = CreateDataTable();
var x = (from r in dt.AsEnumerable()
orderby r.Field<int>("Salary") descending
select r).Take(2);
var s1 = x.ElementAt(0)["Salary"];
var s3 = x.ElementAt(1)["Salary"];
}
private static DataTable CreateDataTable()
{
DataTable dt = new DataTable
{
TableName = "dt1"
};
dt.Columns.Add("Id", typeof(int));
dt.Columns.Add("Name");
dt.Columns.Add("City");
dt.Columns.Add("Department");
dt.Columns.Add("Salary", typeof(int));
dt.Rows.Add(1, "N1", "c1", "d1", 100);
dt.Rows.Add(2, "N2", "c2", "d2", 100);
dt.Rows.Add(3, "N3", "c3", "d3", 150);
dt.Rows.Add(4, "N4", "c4", "d4", 120);
dt.Rows.Add(5, "N5", "c5", "d5", 480);
return dt;
}
get the result:
public static void DatatableTest()
{
var dt = CreateDataTable();
var x = (from r in dt.AsEnumerable()
orderby r.Field<int>("Salary") descending
select r).Take(2);
var s1 = x.ElementAt(0)["Salary"];
var s3 = x.ElementAt(1)["Salary"];
}