List 操作
var customerList = new List<Customer>{
new Customer {Name = "a", Score =30},
new Customer {Name = "a", Score =35},
new Customer {Name = "b", Score =20},
new Customer {Name = "c", Score =50},
new Customer {Name = "b", Score =60},
new Customer {Name = "a", Score =80},
};
// 返回的是分组后,每组个数大于1的list集合
var lll = customerList.GroupBy(x => x.Name).Where(t => t.Count() > 1).SelectMany(x => x).ToList();
//分组后,获取每组score最大的数据重新组合成集合
List<Customer> customers = customerList.GroupBy(t => t.Name).SelectMany(a => a.Where(b => b.Score == a.Max(c => c.Score))).ToList();
//list增加排名序号++y 是从1开始,y++是从0开始
var ncl = customerList.Select((x, y) => new CustomerRank { Rank = ++y, Name = x.Name, Score = x.Score });
public class Customer
{
public string Name;
public int Score;
}
public class CustomerRank : Customer
{
public int Rank { set; get; }
}