繁体   English   中英

Lambda表达式作为函数参数

[英]Lambda expression as function parameter

我有以下代码

List<int> GetIndices<T>(List<T> list, ?????? condition
{
    var result =
            list
                .Select((p, index) => index)
                .Where(condition);

    return result.ToList();
}

我想把它GetIndices(someList, (p, index) => (someList[index].Height < someList[index - 1].Height))

什么是正确的condition

您的代码中存在错误: Where需要一个返回bool值并且list元素类型为输入的委托。

var result = list
   .Select((p, index) => index) // projects the element to it's index (of type int)
   .Where(condition);           // => expects Func<int, bool>

所以你需要Func<int,bool>

但是,根据您的规范,我认为您需要Func<T,int,bool> ,这意味着您必须将GetIndices的实现重写为

var result = list
   .Select((p, index) => new {p, index}) 
   .Where(x => condition(x.p, x.index))
   .Select(x => x.index);  
Func<T, bool>

应该做的伎俩,但你将不得不修改你的lambda因为你不能传递索引(如果你想在Where子句中使用条件)。 您可以轻松地将lambda更改为:

p => someList[someList.IndexOf(p).Height < someList[someList.IndexOf(p)-1].Height

为了将来参考,一旦你学会了如何阅读它(这部分需要一点),扩展方法的MSDN文档是很好的:

MSDN - Enumerable.Where方法

由于这是一个扩展方法,因此第一个参数( IEnumerable<TSource> )是您调用方法的集合(在您的情况下为List<T> )。

第二个参数是您需要匹配的参数。 由于文档调用Func<TSource, bool>TSource在你的情况下是T ...你得到Func<T, bool>

就像jeroenh意识到的那样,你需要捕获原始索引。 您传递的Funct<T,int,bool>条件只需要知道项及其索引,而不是查询中创建的匿名类型,因此传递的条件稍有变化。 它还应该处理索引== 0的情况,因此没有前面的项(索引-1)。

class Program {
    static void Main( string[] args ) {
        var items = Item.GetItems();
        // mind the case where index == 0 so you don't grab an item out of bounds
        var ind = GetIndices( items,
            ( p, index ) => ( h.index == 0 ) ? false : p.Height < items[ index - 1 ].Height );
    }

    static List<int> GetIndices<T>( List<T> list, Func<T, int, bool> condition ) {
        var res = list
            .Select( ( item, index ) => new { item, index } ) // capture original index
            .Where( h => condition( h.item, h.index ) )
            .Select( h => h.index ); // reduce to the index again
        return res.ToList();
    }
}

class Item {
    public int Height {
        get;
        set;
    }
    public Item( int h ) {
        Height = h;
    }
    static public List<Item> GetItems() {
        return new List<Item>( new[]{
                     new Item(1),
                     new Item(4),
                     new Item(2),
                     new Item(5)
        } );
    }
}

试试Func<bool>

或者更确切地说具有正确数量的输入参数的变体

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM