【问题标题】:Type of conditional expression cannot be determined because there is no implicit conversion between 'lambda expression' and 'lambda expression'无法确定条件表达式的类型,因为“lambda 表达式”和“lambda 表达式”之间没有隐式转换
【发布时间】:2015-10-28 03:33:06
【问题描述】:

我有一系列笔记。根据请求这些注释的 UI,我想排除一些类别。这只是一个例子。如果项目 Notes 弹出窗口请求注释,我应该排除 collection 注释。

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

我收到以下错误:Type of conditional expression cannot be determined because there is no implicit conversion between 'lambda expression' and 'lambda expression'

感谢您的帮助

【问题讨论】:

    标签: c# linq


    【解决方案1】:

    编译器不会推断 lambda 表达式的委托类型。您需要在第一个三元子句中使用强制转换来指定委托类型:

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

    一线希望是您可以使用 var 而不必指定变量的类型,因此不会那么冗长。

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2013-08-18
      • 1970-01-01
      • 2018-03-14
      • 2021-12-10
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2015-06-07
      相关资源
      最近更新 更多