[英]Lambda expression as function parameter
I have the following code 我有以下代码
List<int> GetIndices<T>(List<T> list, ?????? condition
{
var result =
list
.Select((p, index) => index)
.Where(condition);
return result.ToList();
}
And I would like to call it like GetIndices(someList, (p, index) => (someList[index].Height < someList[index - 1].Height))
我想把它
GetIndices(someList, (p, index) => (someList[index].Height < someList[index - 1].Height))
What is the correct type of condition
? 什么是正确的
condition
?
There's an error in your code: Where
expects a delegate that returns a bool
value and has the list element type as input. 您的代码中存在错误:
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>
So you would need Func<int,bool>
所以你需要
Func<int,bool>
However, from your spec I think you want Func<T,int,bool>
, which means you have to rewrite your implementation of GetIndices
as 但是,根据您的规范,我认为您需要
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>
Should do the trick but you're going to have to modify your lambda a bit because you can't pass the index (if you want to use condition in the Where
clause). 应该做的伎俩,但你将不得不修改你的lambda因为你不能传递索引(如果你想在
Where
子句中使用条件)。 You could easily change your lambda to: 您可以轻松地将lambda更改为:
p => someList[someList.IndexOf(p).Height < someList[someList.IndexOf(p)-1].Height
For future reference, the MSDN documentation for the extension methods is great once you learn how to read it (that part takes a bit): 为了将来参考,一旦你学会了如何阅读它(这部分需要一点),扩展方法的MSDN文档是很好的:
MSDN - Enumerable.Where Method MSDN - Enumerable.Where方法
Since this is an extension method, the first parameter ( IEnumerable<TSource>
) is the collection you're calling the method on ( List<T>
in your case). 由于这是一个扩展方法,因此第一个参数(
IEnumerable<TSource>
)是您调用方法的集合(在您的情况下为List<T>
)。
The second parameter is what you need to match. 第二个参数是您需要匹配的参数。 Since the documentation calls for
Func<TSource, bool>
and TSource
is T
in your case...you get Func<T, bool>
由于文档调用
Func<TSource, bool>
和TSource
在你的情况下是T
...你得到Func<T, bool>
Like jeroenh realized, you need to capture the original index. 就像jeroenh意识到的那样,你需要捕获原始索引。 The
Funct<T,int,bool>
condition you pass only needs to be aware of the item and its index, not the anonymous type created in the query, so the condition passed changes a bit. 您传递的
Funct<T,int,bool>
条件只需要知道项及其索引,而不是查询中创建的匿名类型,因此传递的条件稍有变化。 It also should handle the situation where the index == 0 and therefore there are no preceding items (index - 1). 它还应该处理索引== 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)
} );
}
}
Try Func<bool>
. 试试
Func<bool>
。
Or rather a variant with the correct amount of input parameters. 或者更确切地说是具有正确数量的输入参数的变体 。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.