在Select扩展方法的重载中,还传递了edindex集合中的当前项目select。这些是它的一些用途。
获取项目的“行号”
var rowNumbers = collection.OrderBy(item => item.Property1) .ThenBy(item => item.Property2) .ThenByDescending(item => item.Property3) .Select((item, index) => new { Item = item, RowNumber = index }) .ToList();
获取项目在其组中的排名
var rankInGroup = collection.GroupBy(item => item.Property1) .OrderBy(group => group.Key) .SelectMany(group => group.OrderBy(item => item.Property2) .ThenByDescending(item => item.Property3) .Select((item, index) => new { Item = item, RankInGroup = index })).ToList();
获取组的排名(在Oracle中也称为density_rank)
var rankOfBelongingGroup = collection.GroupBy(item => item.Property1) .OrderBy(group => group.Key) .Select((group, index) => new { Items = group, Rank = index }) .SelectMany(v => v.Items, (s, i) => new { Item = i, DenseRank = s.Rank }).ToList();
为了进行测试,您可以使用:
public class SomeObject { public int Property1 { get; set; } public int Property2 { get; set; } public int Property3 { get; set; } public override string ToString() { return string.Join(", ", Property1, Property2, Property3); } }
和数据:
List<SomeObject> collection = new List<SomeObject> { new SomeObject { Property1 = 1, Property2 = 1, Property3 = 1}, new SomeObject { Property1 = 1, Property2 = 2, Property3 = 1}, new SomeObject { Property1 = 1, Property2 = 2, Property3 = 2}, new SomeObject { Property1 = 2, Property2 = 1, Property3 = 1}, new SomeObject { Property1 = 2, Property2 = 2, Property3 = 1}, new SomeObject { Property1 = 2, Property2 = 2, Property3 = 1}, new SomeObject { Property1 = 2, Property2 = 3, Property3 = 1} };