Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/shell/5.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
C# 列表<;T>;扩展方法第一、第二、第三……n_C#_Extension Methods - Fatal编程技术网

C# 列表<;T>;扩展方法第一、第二、第三……n

C# 列表<;T>;扩展方法第一、第二、第三……n,c#,extension-methods,C#,Extension Methods,我想访问列表中的第一、第二、第三个元素。我可以使用内置的.First()方法访问第一个元素 我的代码如下: Dictionary<int, Tuple<int, int>> pList = new Dictionary<int, Tuple<int, int>>(); var categoryGroups = pList.Values.GroupBy(t => t.Item1); var highestCount = categoryG

我想访问列表中的第一、第二、第三个元素。我可以使用内置的
.First()
方法访问第一个元素

我的代码如下:

Dictionary<int, Tuple<int, int>> pList = new Dictionary<int, Tuple<int, int>>();  

var categoryGroups = pList.Values.GroupBy(t => t.Item1);
var highestCount = categoryGroups  
                   .OrderByDescending(g => g.Count())  
                   .Select(g => new { Category = g.Key, Count = g.Count() })  
                   .First();  

var 2ndHighestCount = categoryGroups  
                      .OrderByDescending(g => g.Count())  
                      .Select(g => new { Category = g.Key, Count = g.Count() })  
                      .GetNth(1);  

var 3rdHighestCount = categoryGroups  
                      .OrderByDescending(g => g.Count())  
                      .Select(g => new { Category = g.Key, Count = g.Count() })  
                      .GetNth(2);  

twObjClus.WriteLine("--------------------Cluster Label------------------");
twObjClus.WriteLine("\n");  

twObjClus.WriteLine("Category:{0} Count:{1}",  
                    highestCount.Category, highestCount.Count);  
twObjClus.WriteLine("\n");
twObjClus.WriteLine("Category:{0} Count:{1}",  
                     2ndHighestCount.Category, 2ndHighestCount.Count);  
// Error here i.e. "Can't use 2ndHighestCount.Category here"
twObjClus.WriteLine("\n");  

twObjClus.WriteLine("Category:{0} Count:{1}",  
                     3rdHighestCount.Category, 3rdHighestCount.Count);  
// Error here i.e. "Can't use 3rdHighestCount.Category here"
twObjClus.WriteLine("\n");  
  • 我可以将扩展方法写为
    .Second()
    .Third()
    ,类似于 内置方法
    .First()
    来访问第二个和第三个索引

如果您要查找的是单个对象,则无需自己编写,因为这样的对象已经存在

foo.ElementAt(1)
将获得第二个元素,等等。它的工作原理类似于第一个,并返回单个对象


您的
GetNth
方法似乎会返回每个第N个元素,而不仅仅是索引N处的元素。我假设这不是您想要的,因为您说您想要类似于
的东西,因为@Eser放弃了,不想以正确的方式发布答案,下面是:

您应该只进行一次转换,将结果收集到一个数组中,然后从中获取三个元素。现在这样做会导致代码重复以及多次进行分组和排序,这是低效的

var highestCounts = pList.Values
                    .GroupBy(t => t.Item1)
                    .OrderByDescending(g => g.Count())  
                    .Select(g => new { Category = g.Key, Count = g.Count() })  
                    .Take(3)
                    .ToArray();

// highestCounts[0] is the first count
// highestCounts[1] is the second
// highestCounts[2] is the third
// make sure to handle cases where there are less than 3 items!

作为参考,如果有一天您只需要第N个值而不需要前三个值,您可以使用来访问任意索引处的值。

OrderByDescending(…)。选择(…)。取(3)
您只需要索引N处的元素,还是每第N个元素(像每三个一样,因此9个列表将得到3个结果)?因为这就是你的
GetNth
方法所做的。你在说什么错误?@Taufel我想你不明白我的意思。从
pList
开始,进行分组、订购等操作,然后选择3项。就这些。@Taufel想干什么就干什么。。。。什么是“foo”
?OP使用了
GroupBy
s和
OrderByDescending
s,这导致了
IEnumerable
@L.B:一个标准-你肯定见过foo和bar吗?我投了更高的票。虽然我确实发布了一个如何编写扩展方法的示例(这是OP所要求的),但除了这个,真的不需要其他任何东西。如果我们想要一个“安全”的版本,我们可以做一些类似于
.Skip(n).FirstOrDefault()
的事情,但这足够短,它不能保证自己的功能。@Scottannen你看不出真正的问题不是一个扩展方法,是吗?@Scottannen继续回答Y of,但这并不能真正帮助操作(当然,您可以在同一时间获得一些重复次数……).我试过这样做,但这也是我面临的问题,因为只有不到3个项目,得到了一个答案exception@Taufel:我只是通过列表打印结果。打印部分也会复制并粘贴到当前代码中。如果您坚持复制并粘贴,请输入一些ifs来检查数组的长度。您可以不需要在foreach循环中添加任何“如果”。你知道foreach做什么是正确的吗?我认为我很幸运,在观看这些评论时,没有发布答案:)(@MattiVirkkunen你也很幸运免费获得+6)@MattiVirkkunen是的,我知道了,不需要“如果”
var highestCounts = pList.Values
                    .GroupBy(t => t.Item1)
                    .OrderByDescending(g => g.Count())  
                    .Select(g => new { Category = g.Key, Count = g.Count() })  
                    .Take(3)
                    .ToArray();

// highestCounts[0] is the first count
// highestCounts[1] is the second
// highestCounts[2] is the third
// make sure to handle cases where there are less than 3 items!