7

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.

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>

I get an error like this:

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?)`

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.

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.

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?

Josh DeGraw
  • 71
  • 1
  • 6
  • 2
    Maybe you are missing something like `@using System.Data.Linq` a the top of your cshtml file – Hackerman Aug 18 '17 at 20:03
  • 1
    Make sure 'Copy to local' option has been checked for the referenced DLLs such `System.Linq`, 'System.Web.Mvc' ... – Ali Adlavaran Aug 18 '17 at 20:06
  • Just tried adding `@using System.Data.Linq`, it has the same error, it just won't show up. – Josh DeGraw Aug 18 '17 at 20:07
  • 1
    Also make sure you have `Web.config` file inside `Views` folder and also has referenced to System.Linq – Ali Adlavaran Aug 18 '17 at 20:07
  • Also rebuild your solution and make sure has been rebuild successfully. – Ali Adlavaran Aug 18 '17 at 20:08
  • Another approach is to separate the LINQ components into a separate class and call them as methods in your `.cshtml` files. – Peter Smith Aug 18 '17 at 20:19
  • I often miss System.Data.Entity, dont know if that is the case here but... – user2782999 Aug 18 '17 at 20:30
  • 1
    And if you want to do a quick fix do the Where clause in the Controller method instead and pass the result in the ViewBag for example, since you have it working in the .cs files – user2782999 Aug 18 '17 at 20:33
  • Yeah I've used that workaround and it's worked fine, it's just a rather large code base and there are a lot of files where this is a problem so I can't even debug many of the actual pages, and it doesn't make sense to change the whole setup just because it doesn't work in my environment. – Josh DeGraw Aug 18 '17 at 20:35
  • @JoshDeGraw, it actually makes a lot of sense. The View is not supposed to know where the data comes from. That's the whole philosophy behind the MVC pattern. – ataravati Aug 19 '17 at 23:39

2 Answers2

7

Need to add System.Core reference in the main Web.config file.

<compilation>
  <assemblies>
    <add assembly="System.Core, Version=4.0.0.0, Culture=neutral, PublicKeyToken=B77A5C561934E089"/>
  </assemblies>
</compilation>
Rohit Patel
  • 201
  • 3
  • 8
2

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>

enter image description here

andy
  • 435
  • 4
  • 8