Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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

Tags:

c#

linq

I've a collection of notes. Depending on the UI requesting those notes, I'd like to exclude some categories. This is just an example. If the project Notes popup requests notes, I should exclude collection notes.

Func<Note, bool> excludeCollectionCategory = (ui == UIRequestor.ProjectNotes) 
            ? x => x.NoteCategory != "Collections"
            : x => true; //-- error: cannot convert lambda to lambda

I'm getting the following error: Type of conditional expression cannot be determined because there is no implicit conversion between 'lambda expression' and 'lambda expression'

Thanks for helping

like image 892
Richard77 Avatar asked Oct 27 '15 19:10

Richard77


1 Answers

The compiler doesn't infer delegate types for lambda expressions. You need to specify the delegate type using a cast in the first ternary clause:

var excludeCollectionCategory = (ui == UIRequestor.ProjectNotes) 
    ? (Func<Note, bool>)(x => x.NoteCategory != "Collections")
    : x => true;

The silver lining is that you can use var instead of having to specify the type for the variable, so it isn't that much more verbose.

like image 156
Asad Saeeduddin Avatar answered Oct 15 '22 15:10

Asad Saeeduddin