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?