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?
This tutorial teaches you features in .NET Core and the C# language. You’ll learn how to: Generate sequences with LINQ. Write methods that can be easily used in LINQ queries.
It's important to keep in mind that whether you choose to write your LINQ in the query syntax used above or use method syntax instead, it's always possible to go from one form of syntax to the other. The above query written in query syntax can be written in method syntax as:
Now that you know, don't spoil it for everyone else! For more information on LINQ, see:
In code, this means you'll be enumerating both of the sequences you acquired through Take and Skip at once, interleaving the elements, and creating one sequence: your now-shuffled deck of cards. Writing a LINQ method that works with two sequences requires that you understand how IEnumerable<T> works.
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>
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>
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With