Linq查询数据集取得排序后的序列号(行号)
1、测试类 (vs2013)
public class models
{
public int id { set; get; }
public string Name { set; get; }
public string Phone { set; get; }
public string Address { set; get; }
public DateTime Birthday { set; get; }
public static int idx { set; get; }
public static int Idx
{
get { return models.idx; }
set { models.idx = 0; }
}
public models()
{
id = Idx++;
Name = "Name" + id.ToString();
Phone = "Phone" + id.ToString();
Address = "Address" + id.ToString();
Birthday = DateTime.Now.AddHours(id);
}
}实现方法:
var list = new List<models>();
list.Add(new models() {id=10,Name="Lucy",Phone="13545147370" });
list.Add(new models() { id = 10, Name = "Lucy", Phone = "13424241212" });
list.Add(new models() { id = 29, Name = "Jack", Phone = "13867671212" });
list.Add(new models() { id = 19, Name = "bobo", Phone = "13987872323" });
list.Add(new models() { id = 15, Name = "hk", Phone = "13527271818" });
list.Add(new models() { id = 33, Name = "cy", Phone = "15627271616" });
var tt = list.OrderBy(p => p.Name).OrderByDescending(p => p.Birthday).OrderByDescending(p => p.id).Select((p, idx) => new
{
row=idx,
name=p.Name,
phone=p.Phone
});
foreach (var item in tt)
{
Response.Write(item.row + "====" + item.name + "====" + item.phone);
Response.Write("<br />");
}参考:
