Lambda表达式返回错误 [英] Lambda expression returning error

查看:85
本文介绍了Lambda表达式返回错误的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

这是我的代码:

SomeFunction(m => { 
    ViewData["AllEmployees"].Where(c => c.LeaderID == m.UserID); 
 })

,它返回此错误:

并非所有代码路径都在类型为System.Func<IEnumerable>

Not all code paths return a value in lambda expression of type System.Func<IEnumerable>

推荐答案

假设您要返回该.Where()查询的结果,则需要删除括号和分号:

Assuming you're trying to return the result of that .Where() query, you need to drop those braces and that semicolon:

SomeFunction(m => ViewData["AllEmployees"].Where(c => c.LeaderID == m.UserID))

如果将它们放在此处,ViewData[...].Where()将被视为语句而不是表达式,因此您最终得到的lambda在预期的情况下不会返回,从而导致错误.

If you put them there, ViewData[...].Where() will be treated as a statement and not an expression, so you end up with a lambda that doesn't return when it's supposed to, causing the error.

或者,如果您坚持将它们放在此处,则需要一个return关键字,以便该语句实际返回:

Or if you insist on putting them there, you need a return keyword so the statement actually returns:

SomeFunction(m =>
{
    return ViewData["AllEmployees"].Where(c => c.LeaderID == m.UserID);
})

这篇关于Lambda表达式返回错误的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆