The type of conditional expression cannot be determined because there is no implicit conversion between the "lambda expression" and the "lambda expression",

I have a collection of notes. Depending on the user interface requesting these notes, I would like to exclude some categories. This is just an example. If the Notes Notes Notes popup notes notes, I have to exclude the collection notes .

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

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

thanks for the help

+4
source share
1 answer

The compiler does not output delegate types for lambda expressions. You need to specify the type of delegate using the cast in the first triple section:

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

, var , , .

+6

Source: https://habr.com/ru/post/1613401/