Lambda表达式返回错误

编程入门 行业动态 更新时间:2024-10-20 05:22:49
本文介绍了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表达式返回错误

本文发布于:2023-11-08 07:36:37,感谢您对本站的认可!
本文链接:https://www.elefans.com/category/jswz/34/1568749.html
版权声明:本站内容均来自互联网,仅供演示用,请勿用于商业和其他非法用途。如果侵犯了您的权益请与我们联系,我们将在24小时内删除。
本文标签:表达式   错误   Lambda

发布评论

评论列表 (有 0 条评论)
草根站长

>www.elefans.com

编程频道|电子爱好者 - 技术资讯及电子产品介绍!