我知道我可以定义一个谓词:
Dim _predicate As New Func(Of T, Boolean)(Function(e)e.Foo= "Value")
并将其传递给LINQ方法:
Dim _x as String = enumerable.First(predicate).Foo
我想抽出"价值"字符串,所以我创建了:
Dim _delegate As New Func(Of T, String, Boolean)(Function(e, s)e.Foo= s)
如何将此委托传递给LINQ函数,传递字符串参数但仍然假设T参数每次都是迭代器项?
我想没有可用的过载需要这样的委托,但我真的不想为所有LINQ方法编写自己的重载...
我想我想要的是一种将委托转换为谓词的内联方式,提供替换该额外参数的值。
答案 0 :(得分:1)
抱歉,我不是VB人,但我确信我的代码可以轻松翻译:
class ValueFilter<T>
where T : IFooThing
{
public ValueFilter(string value)
{
_value = value;
}
private readonly string _value;
public IsMatch(T item) => (item.Foo == _value);
}
然后
var filter = new ValueFilter("A value");
IEnumerable<Foo> collection = ...
collection.First(filter.IsMatch);
(假设IFooThing
具有public string Foo { get; }
属性