I would like to check the same field in Datatable, and I want to setup a number for each row which starts from Zero, if same field found, number + 1
Original Table
Name
==========
John
John
John
Tommy
Tommy
Andy
How to setup number like :
Name | Number
=================
John | 0
John | 1
John | 2
Tommy | 0
Tommy | 1
Andy | 0
Any ideas for this? Thanks for your help.
Try this:
var Rank = datatable1.GroupBy(x => x.Name)
.Select(g => new {g, count= g.Count()})
.SelectMany(t => t.g.Select(b => b)
.Zip(Enumerable.Range(1,t.count), (j,i) => new {j.Name, rn = i}));
foreach (var i in Rank)
{
Console.WriteLine("{0} {1}",i.Name, i.rn);
}
Output:
John | 1
John | 2
John | 3
Tommy | 1
Tommy | 2
Andy | 1
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.