1

I found the following example which works well:

private static bool GreaterTwo(int arg)
{
    return arg > 2;
}

public static void GreaterTwoSample()
{
    Enumerable.Range(0, 10).Where(GreaterTwo);
}

What would be the syntax for using GreaterTwo with another parameter within the WHERE-Lambda:

private static bool GreaterTwoSmallerArg2(int arg, int arg2)
{
    return arg > 2 && arg < arg2;
}

UPDATE: thanks to HugoRune's answer, here is another working example with 2 parameters

public static void LimitationSample()
{
    Enumerable.Range(0, 10).Where(IsBetween(2, 5));
}
private static Func<int, bool> IsBetween(int min, int max)
{
    return x  => x > min && x < max;
}

3 Answers 3

3

You could try something like below:

public static void GreaterTwoSample(int arg2)
{
    Enumerable.Range(0, 10).Where(x=>GreaterTwoSmallerArg2(x, arg2));
}
Sign up to request clarification or add additional context in comments.

4 Comments

+1 this is the correct answer you may want to add that Where(GreaterTwo); is a syntactic sugar for Where(x => GreaterTwo(x));
@Selman22 .. and i guess there is no sugar-way for a second parameter?
@Selman22 Behavior of these two syntaxes is similar, but different, in some cases very different.
@fubo is you mean just passing the method name instead of using a delegate, no, since the signature for GreaterTwoSmallerArg2 does not match the signature for the parameter to Where.
1

Enumerable.Range(0, 10).Where(...) requires a function that takes a single int as a parameter and returns a bool, there is no way around that.

The two usual ways to do this are:

  • pass the name of an existing function that takes an int, i.e.

    Enumerable.Range(0, 10).Where(YourFunctionThatTakesAnIntAndReturnsBool);
    
  • pass a lambda expression

    Enumerable.Range(0, 10).Where(x=>{Expression_that_returns_a_bool})
    

So the basic solution if you want to use a function that needs two integers is to pack it into a lambda expression i.e

Enumerable.Range(0, 10).Where(x=>GreaterTwoSmallerArg2(x, 5))

But if you want more syntactic sugar, you can make a function that returns a function,

private static Func<int,bool> funcGreaterTwoSmallerArg2(int arg2)
{
    return x => GreaterTwoSmallerArg2(x, arg2);
    // OR: return x => x > 2 && x < arg2;
}

Then you can use it like this:

Enumerable.Range(0, 10).Where(funcGreaterTwoSmallerArg2(5));

This syntax should be used with care, it can make the source hard to understand. But sometimes it is warranted, if you reuse a specific lambda often enough, or it is particularly complicated.

3 Comments

i'm new to this, i transfered your example to one with 2 Parameters in my answer. What does x and y stand for?
The x and y stand for the two int in Func<int,int,bool>. (x,y) => x > min && x < max; is equivalent to bool myFunc(int x,int y){return x>min&&x<max;}. However the y seems unnecessary in your case, I am not sure why you would need it. And I think the example you provided will not work, Enumerable.Range(0, 10).Where() requires a Func<int,bool>, not Func<int,int,bool>
Regarding your earlier edit: There actually is an overload for Where that takes two int: the second int is the index of the element. So you would use that if for some reason you wanted to compare not only the value of each element in your list, but also its position.
1

Where<T> takes a delegate that has a single T input and a bool output (aka predicate). To use a method that has two parameters you'd have to project to it using a lambda:

private static bool GreaterTwoSmallerArg2(int arg, int arg2)
{
    return arg > 2 && arg < arg2;
}

public static void GreaterTwoSample()
{
    Enumerable.Range(0, 10).Where(i => GreaterTwoSmallerArg2(i, {some other value));
}

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.