C# LINQ:有没有一种方法可以向where子句提供具有多个参数的谓词

C# LINQ:有没有一种方法可以向where子句提供具有多个参数的谓词,c#,linq,where,C#,Linq,Where,想知道是否有办法做到以下几点: 我基本上想为where子句提供一个谓词,其中包含多个参数,如下所示: public bool Predicate (string a, object obj) { // blah blah } public void Test() { var obj = "Object"; var items = new string[]{"a", "b", "c"}; var result = items.Where(Predicate);

想知道是否有办法做到以下几点: 我基本上想为where子句提供一个谓词,其中包含多个参数,如下所示:

public bool Predicate (string a, object obj)
{
  // blah blah    
}

public void Test()
{
    var obj = "Object";
    var items = new string[]{"a", "b", "c"};
    var result = items.Where(Predicate); // here I want to somehow supply obj to Predicate as the second argument
}

你期待这样的事情吗

        public bool Predicate (string a, object obj)
        {
          // blah blah    
        }

        public void Test()
        {
            var obj = "Object";
            var items = new string[]{"a", "b", "c"};
            var result = items.Where(x => Predicate(x, obj)); // here I want to somehow supply obj to Predicate as the second argument
        }

您想要的操作称为“部分评估”;它在逻辑上与将一个双参数函数“转换”为两个单参数函数有关

static class Extensions
{
  static Func<A, R> PartiallyEvaluateRight<A, B, R>(this Func<A, B, R> f, B b)
  {
    return a => f(a, b);
  }
}
...
Func<int, int, bool> isGreater = (x, y) => x > y;
Func<int, bool> isGreaterThanTwo = isGreater.PartiallyEvaluateRight(2);
现在您可以创建一个工厂:

Func<int, int, bool> greaterThan = (x, y) => x > y;
Func<int, Func<int, bool>> factory = greaterThan.Curry();
Func<int, bool> withTwo = factory(2); // makes y => 2 > y
Func greaterThan=(x,y)=>x>y;
Func factory=greaterThan.Curry();
Func with two=工厂(2);//使y=>2>y

清楚了吗?

这是一种方法。但是我期待C++中的BID2。将二元谓词转换为一元谓词
static class Extensions
{
  static Func<A, Func<B, R>> Curry<A, B, R>(this Func<A, B, R> f)
  {
    return a => b => f(a, b);
  }
}
Func<int, int, bool> greaterThan = (x, y) => x > y;
Func<int, Func<int, bool>> factory = greaterThan.Curry();
Func<int, bool> withTwo = factory(2); // makes y => 2 > y