How can I assign a Func<> conditionally between lambdas using the conditional ternary operator?

C#LambdaConditional Operator

C# Problem Overview


Generally, when using the conditional operator, here's the syntax:

int x = 6;
int y = x == 6 ? 5 : 9;

Nothing fancy, pretty straight forward.

Now, let's try to use this when assigning a Lambda to a Func type. Let me explain:

Func<Order, bool> predicate = id == null
    ? p => p.EmployeeID == null
    : p => p.EmployeeID == id;

That's the same syntax, and should work? Right? For some reason that doesn't. The compiler gives this nice cryptic message:

>Error 1 Type of conditional expression cannot be determined because there is no implicit conversion between 'lambda expression' and 'lambda expression'

I then went ahead and changed the syntax and this way it did work:

Func<Order, bool> predicate = id == null
    ? predicate = p => p.EmployeeID == null
    : predicate = p => p.EmployeeID == id;

I'm just curious as to why it doesn't work the first way?

(Side note: I ended up not needing this code, as I found out that when comparing an int value against null, you just use object.Equals)

C# Solutions


Solution 1 - C#

You can convert a lambda expression to a particular target delegate type, but in order to determine the type of the conditional expression, the compiler needs to know the type of each of the second and third operands. While they're both just "lambda expression" there's no conversion from one to the other, so the compiler can't do anything useful.

I wouldn't suggest using an assignment, however - a cast is more obvious:

Func<Order, bool> predicate = id == null 
    ? (Func<Order, bool>) (p => p.EmployeeID == null)
    : p => p.EmployeeID == id;

Note that you only need to provide it for one operand, so the compiler can perform the conversion from the other lambda expression.

Solution 2 - C#

The C# compiler cannot infer the type of the created lambda expression because it processes the ternary first and then the assignment. you could also do:

Func<Order, bool> predicate = 
    id == null ? 
        new Func<Order,bool>(p => p.EmployeeID == null) :
        new Func<Order,bool>(p => p.EmployeeID == id);

but that just sucks, you could also try

Func<Order, bool> predicate = 
    id == null ? 
        (Order p) => p.EmployeeID == null :
        (Order p) => p.EmployeeID == id;

Solution 3 - C#

Let me have my own example since I had the same problem, too (with the hope that the example be helpful for others):

My Find method is generic method that gets Expression<Func<T, bool>> as predicate and gives List<T> as output.
I wanted to find countries, but I need all of them if language list was empty, and filtered list, if language list was filled. First I used the Code as below:

var countries= 
Find(languages.Any() 
  ? (country => languages.Contains(country.Language))
  : (country => true));

But exactly I get the error :there is no implicit conversion between lambda expression and lambda expression.

The problem was that, we have just two lambda expressions here, and nothing else, for example, what is country => true exactly?? We have to determine the type of at least one of lambda expressions. If just of one of the expressions be determined, then the error will be omitted. But for make the code more readable, I extracted both lambda expressions, and used the variable instead, as below:

   Expression<Func<Country, bool>> getAllPredicate = country => true;
   Expression<Func<Country, bool>> getCountriesByLanguagePredicate = country => languages.Contains(country.Language);

   var countries= Find(languages.Any()
                       ? getCountriesByLanguagePredicate
                       : getAllPredicate);

I emphasize that, if I just determined one of the expression's type, the error will be fixed.

Solution 4 - C#

Just an update - in C# 10, it IS now possible for the compiler to infer the 'natural type' of a lambda, provided that the input type(s) are provided, e.g.

var evenFilter = (int i) => i % 2 == 0; // evenFilter inferred as `Func<int, bool>`

This also means that 0 input Funcs and Actions can be inferred:

var zeroInputFunc = () => 44 % 2 == 0;
var myAction = () => {Console.WriteLine("Foo");};

However, this won't work:

var filter = i => i % 2 == 0; << Error: The delegate type could not be inferred

As a result, it is now possible to do what the OP originally wanted to do, provided that at least the input types are provided, e.g.

Func<int, bool> myPredicate = selectorFlag
	? i => i % 2 == 0
	: i => i % 2 == 1;

However, this still isn't permitted:

var myPredicate = selectorFlag
	? (int i) => i % 2 == 0
	: (int i) => i % 2 == 1;

> Error : no implicit conversion between 'lambda expression' and 'lambda expression'

Attributions

All content for this solution is sourced from the original question on Stackoverflow.

The content on this page is licensed under the Attribution-ShareAlike 4.0 International (CC BY-SA 4.0) license.

Content TypeOriginal AuthorOriginal Content on Stackoverflow
QuestionBFreeView Question on Stackoverflow
Solution 1 - C#Jon SkeetView Answer on Stackoverflow
Solution 2 - C#JakeView Answer on Stackoverflow
Solution 3 - C#ElnazView Answer on Stackoverflow
Solution 4 - C#StuartLCView Answer on Stackoverflow