为什么我无法使LINQ在我的.cshtml文件中工作? [英] Why can't I get LINQ to work in my .cshtml files?

查看:61
本文介绍了为什么我无法使LINQ在我的.cshtml文件中工作?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在VS 2017中工作,目标是.NET Framework 4.6.2,我已经在几个不同的项目中遇到了这个问题.我相信这些项目最初是在VS 2012中创建的,这是值得的.

I'm working in VS 2017, targeting .NET Framework 4.6.2, and I've been running into this in a few different projects. I believe the projects were initially created in VS 2012, for what it's worth.

在任何.cs文件中,我都可以使用Linq扩展名和查询语法,而没有任何问题,但是如果我在.cshtml剃刀文件中尝试这样的一行:

I'm able to use Linq extensions and query syntax with no problems whatsoever in any of the .cs files, but if I try a line like this in a .cshtml razor file:

@{
    var activeCategories =  Model.Categories.Where(x => x.Articles.Count > 0).Count();
 }

<p>Active Categories: @activeCategories</p>

我收到这样的错误:

List<Category> does not contain a definition for 'Where', and the closest Extension method 
accepting a first argument of type List<Category> could not be found (are you missing a 
using directive or an assembly reference?)`

我尝试将using System.Linq;添加到.cshtml文件的顶部,但是显示了找不到的错误.

I've tried adding using System.Linq; to the top of the .cshtml files, but it shows an error that it can not be found.

我已经将Views文件夹下的Web.config更新为包含System.Linq,已经检查并且项目引用了System.Core,并且清除了%LOCALAPPDATA%/Microsoft/VisualStudio/15.0_ba2c3fe6/ComponentModelCache中的项目.

I've updated the Web.config under the Views folder to include System.Linq, I've checked and the project is referencing System.Core, and I've cleared the items in %LOCALAPPDATA%/Microsoft/VisualStudio/15.0_ba2c3fe6/ComponentModelCache.

关于是否在视图中使用LINQ的所有哲学,这不应该发生,对吗?当我使用这些较旧的项目创建一个新的ASP.NET MVC项目时,这不是问题.

All philosophy about whether or not to use LINQ in the views aside, this shouldn't be happening, right? This isn't a problem when I create a new ASP.NET MVC project, just with these older projects.

我还假定这与我的本地环境有关,因为其他开发人员似乎在他们的计算机上没有此问题.

I also am assuming this is something to do with my local environment, as the other devs don't seem to have this issue on their machines.

为什么会这样,我该如何解决?

Why is this happening, and how can I fix it?

推荐答案

我也遇到了同样的问题,我尝试将这些问题添加到webconfig中,然后重新构建,再次打开cshtml,它对我有用!!

I have same problem, and I try to add these in webconfig, then rebuild, open the cshtml again, it's working for me!!

<compilation debug="true" targetFramework="4.5">
  <assemblies>
    <add assembly="System.Core, Version=4.0.0.0, Culture=neutral, PublicKeyToken=b77a5c561934e089"/>
    <add assembly="System.Data, Version=4.0.0.0, Culture=neutral, PublicKeyToken=b77a5c561934e089"/>
    <add assembly="Microsoft.CSharp, Version=4.0.0.0, Culture=neutral, PublicKeyToken=b03f5f7f11d50a3a"/>
    <add assembly="System.Runtime, Version=4.0.0.0, Culture=neutral, PublicKeyToken=b03f5f7f11d50a3a" />
    <add assembly="System.Web.Abstractions, Version=4.0.0.0, Culture=neutral, PublicKeyToken=31BF3856AD364E35" />
    <add assembly="System.Web.Helpers, Version=3.0.0.0, Culture=neutral, PublicKeyToken=31BF3856AD364E35" />
    <add assembly="System.Web.Routing, Version=4.0.0.0, Culture=neutral, PublicKeyToken=31BF3856AD364E35" />
    <add assembly="System.Web.WebPages, Version=3.0.0.0, Culture=neutral, PublicKeyToken=31BF3856AD364E35" />
    <add assembly="System.Web.Mvc, Version=5.2.3.0, Culture=neutral, PublicKeyToken=31BF3856AD364E35" />
  </assemblies>
</compilation>

这篇关于为什么我无法使LINQ在我的.cshtml文件中工作?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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