我有以下代码
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
的委托(delegate)值并将列表元素类型作为输入。
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);
关于c# - Lambda 表达式作为函数参数,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/5965985/