在这个
post中,问题的解决办法是:
list.Where((item,index)=> index< list.Count - 1&& list [index 1] == item) 多参数(即(项目,索引))的概念对我来说有些困惑,我不知道正确的单词来缩小我的google结果.所以1)这叫什么?更重要的是,2)不可枚举的变量如何初始化?在这种情况下,如何将索引编译为int并初始化为0? 谢谢.
解决方法
Lambda表达式具有各种语法选项:
() => ... // no parameters x => ... // single parameter named x,compiler infers type (x) => ... // single parameter named x,compiler infers type (int x) => ... // single parameter named x,explicit type (x,y) => ... // two parameters,x and y; compiler infers types (int x,string y) => ... // two parameters,x and y; explicit types
这里的微妙之处在于,哪里有一个负载分别接受Func< T,int,bool>代表值和索引(并返回匹配的bool).所以这是提供索引的Where实现,就像:
static class Example { public static IEnumerable<T> Where<T>(this IEnumerable<T> source,Func<T,bool> predicate) { int index = 0; foreach (var item in source) { if (predicate(item,index++)) yield return item; } } }
原文地址:https://www.jb51.cc/csharp/94384.html
版权声明:本文内容由互联网用户自发贡献,该文观点与技术仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌侵权/违法违规的内容, 请发送邮件至 dio@foxmail.com 举报,一经查实,本站将立刻删除。