ASP.NET MVC:不能使用lambda前pression作为参数传递给动态调度的操作 [英] ASP.NET MVC: Cannot use a lambda expression as an argument to a dynamically dispatched operation

查看:434
本文介绍了ASP.NET MVC:不能使用lambda前pression作为参数传递给动态调度的操作的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个ASP.NET应用程序MVC4。
我的看法得到我的控制器列表。
我想选择这些清单,拉姆达前pression,但我得到了以下错误:

不能使用lambda前pression作为参数传递给动态调度的操作,而不首先将其转换为一个委托或前pression树型

 列表< project.Models.LAYER>层=新的List< project.Models.LAYER>();
层= @ M​​odel.layers.Select(X => x.KONT ==欧);

@ Model.layers是一个List

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

  @ {
  清单< project.Models.LAYER>层= Model.layers.Where(X => x.KNOT ==欧洲)了ToList();
}


解决方案

它看起来像你在你看来,这违反关注点分离的原则,这样做。但是,这是你会怎么做。

  @
{
   VAR层= Model.layers.Where(X => x.KONT ==欧洲)了ToList();
}@foreach(分层VAR层)
{
  .....
}

更好的方式

不过什么,你应该做的是在你的模型GetLayersForLocation创建一个方法那么你的code是这样的:

在模型类

 公开的IEnumerable<层> GetLayersForLocation(字符串位置)
{
    返回this.layers.Where(X => x.Knot ==位置);
}

您认为code

  @foreach(在Model.GetLayersForLocation VAR层(欧))
{
  .....
}

究其原因,这是更好的是,您现在可以进行单元测试code,你不能因为它只是你的视图的一部分之前会,但现在你可以运行自动测试,以确保得到适当的层工作中。

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:

Cannot use a lambda expression as an argument to a dynamically dispatched operation without first casting it to a delegate or expression tree type

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

@Model.layers is a List

NOW I TRIED THAT: BUT THE SAME ERROR:

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

解决方案

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:

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.

这篇关于ASP.NET MVC:不能使用lambda前pression作为参数传递给动态调度的操作的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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