Display Row_number using Linq Query
1. Add following class
public class Emp
{
public int ID { get; set; }
public string Name { get; set; }
public string City { get; set; }
public Emp(int id, string name, string city)
{
this.ID = id;
this.Name = name;
this.City = city;
}
}
2. Creating List
protected List<Emp> GetEmpList()
{
List<Emp> lEmp = new List<Emp>();
Emp oemp = new Emp(1234, "Devesh Omar", "GZB");
lEmp.Add(oemp);
oemp = new Emp(1234, "ROLI", "GZB");
lEmp.Add(oemp);
oemp = new Emp(1235, "ROLI", "MainPuri");
lEmp.Add(oemp);
return lEmp;
}
3. Linq Query
List<Emp> Lstemp = GetEmpList();
int Srno = 0;
var columns = from t in Lstemp
orderby t.Name
select new
{
Row_number=++Srno,
EmpID = t.ID,
Name = t.Name,
City = t.City
};
4. using Row_number=++Srno it will display row number in output