如何选择列表中的热门项目 [英] How Do I Select Top Items in List
本文介绍了如何选择列表中的热门项目的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
我在下面有这个清单。
I have this List below.
class Program
{
static void Main()
{
List<int> list = new List<int>();
list.Add(1);
list.Add(2);
list.Add(3);
list.Add(4);
list.Add(5);
// Remove all except first 3
foreach (int i in list)
{
Console.Write(i);
}
}
}
我该如何为它实现代码?需要帮助。
提前谢谢。
How do I implement the code for it? Help needed.
Thanks in advance.
推荐答案
你可以使用Linq(take)获取列表中的前3个元素:
http://msdn.microsoft.com/fr-fr/library/bb503062(v = vs.110).aspx [ ^ ]
You can use Linq (take) to get the top 3 elements from the list:
http://msdn.microsoft.com/fr-fr/library/bb503062(v=vs.110).aspx[^]
var firstThree = list.Take(3);
试试这个:
Try this:
List<int> list = new List<int>();
list.Add(1);
list.Add(2);
list.Add(3);
list.Add(4);
list.Add(5);
int remove = Math.Max(0, list.Count - 3);
list.RemoveRange(3,remove);
foreach (int i in list)
{
Console.Write(i);
}
这篇关于如何选择列表中的热门项目的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
查看全文