I’m trying to write a lambda like below but my syntax is incorrect.
Result = ListOfNumbers.Where(val => { val != Num1; val != Num2; }).ToList()[0];
The error I get is
Not all code paths return a value in lambda expression of type
‘System.Func<int,int,bool>‘
Perhaps there’s a better way of doing this… I know there’s the numbers 1, 2 and 3 (in that order) in ListOfNumbers. Num1 and Num2 at this point will both be either 1, 2 or 3 (they can’t be the same though). I want my result to be the ‘other’ number from ListOfNumbers. Hope that’s clear. If you can think of a neater way of doing it I’d love to hear it.
Any thoughts?
you need to change it to
If I write the code you’ve provided as it would be as a function you might see what’s wrong:
I.e. – where’s the return statement?
Although – note that that will actually fail to compile with the error
Only assignment, call, increment, decrement, and new object expressions can be used as a statement– the compiler rules in lambdas are slightly different and so you get a different error but ultimately it’s for the same kind of reason – in your case none of your lambda paths return anything.