简体   繁体   English

@foreach循环-LINQ /剃刀

[英]@foreach loop - linq / razor

i use this code to filter the items in the list. 我使用此代码来过滤列表中的项目。

@foreach (var wu in ViewBag.GetWorkUser.Where(op => op.action == 0))
        {@wu.DisplayName}

code: linq / Razor 代码:linq /剃刀

but i getting this error 但我得到这个错误

Cannot use a lambda expression as an argument to a dynamically dispatched operation without first casting it to a delegate or expression tree type 在未先将lambda表达式转换为委托或表达式树类型之前,不能将lambda表达式用作动态调度操作的参数

i can´t understand what is wrong here. 我不明白这里出了什么问题。 i have search and try some other code to solved this problem but still the same error. 我已经搜索并尝试其他一些代码来解决此问题,但仍然是相同的错误。

someone can give me a hand with this pls? 有人可以帮我这个忙吗?

ViewBag is a dynamic collection. ViewBag是一个动态集合。 So the type of op (lambda variable) is not known at compile time, you need to cast ViewBag.GetWorkUser before using Where like this: 因此,在编译时不知道op的类型(lambda变量),需要在使用Where之前ViewBag.GetWorkUser ,如下所示:

((IEnumerable<User>)ViewBag.GetWorkUser).Where(op => op.action == 0))
        //    ^^^ User or whatever type you have
@foreach (var wu in ((IEnumerable<User>)ViewBag.GetWorkUser).Where(op => op.action == 0))
{
    wu.DisplayName
}

The correctly way sothing like this: 正确的方法是这样的:

@foreach (var wu in ((List<GetWorkUser>)ViewBag.GetWorkUser).Where(op => op.action == 0)){
    <span> @wu.DisplayName </span>
}

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM