无法确定条件表达式的类型,因为'lambda expression'和'lambda expression'之间没有隐式转换

无法确定条件表达式的类型,因为'lambda expression'和'lambda expression'之间没有隐式转换

Ric*_*d77  5 c# linq

 

我收集了一些笔记.根据请求这些笔记的UI,我想排除某些类别.这只是一个例子.如果项目Notes弹出请求备注,我应该排除集合备注.

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'

谢谢你的帮助

 

Asa*_*din  8

 

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

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

一线希望是你可以使用var而不必指定变量的类型,所以它不是那么冗长.

 

 

之前的写法

Expression<Func<Math_UserInfo, bool>> where3 = i => true;
if (!string.IsNullOrEmpty(User_Dept_Id))
{

Guid sth = Guid.Parse(User_Dept_Id);
where3 = i => i.User_Dept_Id == sth;
}

 

现在的写法

Expression<Func<Math_UserInfo, bool>> where4 = !string.IsNullOrEmpty(UserPhone)? (Expression<Func<Math_UserInfo, bool>>)(i => i.UserPhone.Contains(UserPhone)): i => true;

 

posted on 2023-02-15 12:40  漫思  阅读(59)  评论(1编辑  收藏  举报

导航