C# ASP.NET MVC:不能使用 lambda 表达式作为动态调度操作的参数

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/17083616/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-10 08:32:01  来源:igfitidea点击:

ASP.NET MVC: Cannot use a lambda expression as an argument to a dynamically dispatched operation

c#asp.netasp.net-mvcasp.net-mvc-4razor

提问by alexander-fire

I have a ASP.NET MVC4 Application. My view get a List from my controller. I want to select these list with lambda expression but I get the following error:

我有一个 ASP.NET MVC4 应用程序。我的视图从我的控制器获取一个列表。我想用 lambda 表达式选择这些列表,但出现以下错误:

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 表达式用作动态调度操作的参数

List<project.Models.LAYER> layers = new List<project.Models.LAYER>();
layers = @Model.layers.Select(x => x.KONT == "EUROPE");

@Model.layers is a List

@Model.layers 是一个列表

NOW I TRIED THAT: BUT THE SAME ERROR:

现在我试过了:但同样的错误:

@{
  List<project.Models.LAYER> layers = Model.layers.Where(x => x.KNOT == "EUROPE").ToList();
}

采纳答案by nerdybeardo

It looks like you're doing this in your view, which violates the principles of separation of concerns. But this is how you would do it.

在您看来,您这样做似乎违反了关注点分离的原则。但这就是你要做的。

@
{
   var layers = Model.layers.Where(x => x.KONT == "EUROPE").ToList();
}

@foreach(var layer in layers)
{
  .....
}

A Better Way

更好的方式

What you should do however is create a method on your Model "GetLayersForLocation" Then your code would look like this:

但是你应该做的是在你的模型“GetLayersForLocation”上创建一个方法然后你的代码看起来像这样:

In Your Model Class

在您的模型类中

public IEnumerable<Layer> GetLayersForLocation(string location)
{
    return this.layers.Where(x => x.Knot == location);
}

In Your View Code

在您的视图代码中

@foreach(var layer in Model.GetLayersForLocation("EUROPE"))
{
  .....
}

The reason this is better is you can now unit test your code, before you wouldn't be able to because it's just part of your view, but now you can run automated tests to ensure that getting the proper layers is working.

这样做更好的原因是您现在可以对您的代码进行单元测试,因为它只是您视图的一部分,而您现在无法进行单元测试,但现在您可以运行自动化测试以确保获得正确的层正在工作。

回答by Jason Li

  1. layers is a List, Model.layers.Select will return an IEnumerable.

  2. If you only want to return layer whose KONT == ‘EUROPE', you should use like following

    layers = @Model.layers.Where(x => x.KNOT == "EUROPE").ToList();
    
  1. 层是一个列表,Model.layers.Select 将返回一个 IEnumerable.

  2. 如果只想返回其图层KONT == ‘EUROPE',则应使用如下所示

    layers = @Model.layers.Where(x => x.KNOT == "EUROPE").ToList();
    

回答by Greg

For others, I've notice I get this error in Views when I do not have a strongly typed view, such as if a single character accidentally gets typed before the "@model type"line (and thus the model type declaration is now no longer being made.)

对于其他人,我注意到当我没有强类型视图时,我在视图中收到此错误,例如如果在“@model type”之前意外输入了单个字符(因此模型类型声明现在不是制作时间更长。)

 @model SomeModel