您的位置:首页 > 编程语言 > C#

c# 对list 操作的写法总结

2017-08-23 14:41 218 查看
1:统计list 内重复值的数量

List<int> list = new List<int>() { 12, 12, 13, 13, 14, 15, 15, 15 };

var g = list.GroupBy(i => i);

foreach (var item in g)
{
Console.WriteLine("Value:{0} , Count:{1}", item.Key, item.Count());
}


结果:
Value:12 , Count:2
Value:13 , Count:2
Value:14 , Count:1
Value:15 , Count:3


2:统计list内某个值的数量

list.FindAll(delegate(int n) { return n == 100; }).Count

List<int> list = new List<int>();
list.Add(100);
list.Add(100);
list.Add(100);
list.Add(100);
list.Add(100);
MessageBox.Show(list.FindAll((ex) => { return ex == 100; }).Count.ToString());
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: