请解释System.Linq.Enumerable.Where(Func< T,int,bool>谓词) [英] Please explain System.Linq.Enumerable.Where(Func<T, int, bool> predicate)

查看:179
本文介绍了请解释System.Linq.Enumerable.Where(Func< T,int,bool>谓词)的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

对于这样的Where方法的重载,我无法理解MSDN文档,Where方法接受具有两个参数的谓词,其中int可以表示源元素的索引,无论这是什么意思(我认为可枚举是一个序列,您看不到下一个项目,更不用说对它进行任何索引了.

I can't make any sense of the MSDN documentation for this overload of the Where method that accepts a predicate that has two arguments where the int, supposedly, represents the index of the source element, whatever that means (I thought an enumerable was a sequence and you couldn't see further than the next item, much less do any indexing on it).

有人可以解释一下如何使用此重载,特别是Func中的int的用途是什么以及如何使用吗?

Can someone please explain how to use this overload and specifically what that int in the Func is for and how it is used?

推荐答案

int参数表示当前迭代中当前项目的索引.每次调用LINQ扩展方法之一时,理论上都不能保证以相同的顺序返回项目,但是您知道它们每个都被返回一次,因此可以为其分配索引. (好吧,如果您知道查询对象是List<T>之类的东西,但可以肯定地说,但是可以保证.)

The int parameter represents the index of the current item within the current iteration. Each time you call one of the LINQ extension methods, you aren't in theory guaranteed to get the items returned in the same order, but you know they're all be returned once each and thus can be assigned indices. (Well, you are guaranteed if you know the query object is a List<T> or such, but not in general.)

示例:

var result1 = myEnumerable.Where((item, index) => index < 4);
var result2 = myEnumerable.Take(4);
// result1 and result2 are equivalent.

这篇关于请解释System.Linq.Enumerable.Where(Func&lt; T,int,bool&gt;谓词)的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
相关文章
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆