加入收藏 | 设为首页 | 会员中心 | 我要投稿 李大同 (https://www.lidatong.com.cn/)- 科技、建站、经验、云计算、5G、大数据,站长网!
当前位置: 首页 > 百科 > 正文

c# – 多参数linq表达式如何初始化其参数?

发布时间:2020-12-15 06:51:24 所属栏目:百科 来源:网络整理
导读:在这个 post中,问题的解决办法是: list.Where((item,index)= index list.Count - 1 list [index 1] == item) 多参数(即(项目,索引))的概念对我来说有些困惑,我不知道正确的单词来缩小我的google结果.所以1)这叫什么?更重要的是,2)不可枚举的变量如何初始化
在这个 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;
        }
    }
}

(编辑:李大同)

【声明】本站内容均来自网络,其相关言论仅代表作者个人观点,不代表本站立场。若无意侵犯到您的权利,请及时与联系站长删除相关内容!

    推荐文章
      热点阅读