Brian Mains
Brian Mains

Reputation: 50728

LINQ in Razor View Broken for VB

I'm trying to use LINQ in a Razor view (VB syntax) and I have the following:

Model.TypeList.Select(Function(i) ...)

Syntactically, everything is correct; the statement is correct, and the Model types are defined correct. However, I get the following error within design-time:

Error   12  'Select' is not a member of 'System.Collections.Generic.List(Of TypeItem)'

Obviously Select is a LINQ extension method, but it's not supported in my view, even though the DLL reference is there, and I added it as a namespace to the web.config files... Why is this basic feature not working?

I am using ASP.NET MVC 5.0, and note I'm using a custom base page class. EDIT: In the view I am using, I have the following imports:

@Imports System.Linq
@Imports System.Collections.Generic

My root web.config file has the following:

<system.web>
   <compilation debug="true" targetFramework="4.5.1" />
    <httpRuntime targetFramework="4.5" />
   <pages>
      <namespaces>
        <add namespace="System.Collections.Generic"/>
        <add namespace="System.Linq"/>
        <add namespace="System.Web.Helpers" />
        <add namespace="System.Web.Mvc" />
        <add namespace="System.Web.Mvc.Ajax" />
        <add namespace="System.Web.Mvc.Html" />
        <add namespace="System.Web.Optimization" />
        <add namespace="System.Web.Routing" />
        <add namespace="System.Web.WebPages" />
            </namespaces>
    </pages>

My views wb.config has the following:

 <system.web.webPages.razor>
    <host factoryType="System.Web.Mvc.MvcWebRazorHostFactory, System.Web.Mvc, Version=5.0.0.0, Culture=neutral, PublicKeyToken=31BF3856AD364E35" />
    <pages pageBaseType="Custom.MvcViewPage">
      <namespaces>
        <add namespace="System.Collections.Generic"/>
        <add namespace="System.Linq"/>
        <add namespace="System.Web.Mvc" />
        <add namespace="System.Web.Mvc.Ajax" />
        <add namespace="System.Web.Mvc.Html" />
        <add namespace="System.Web.Optimization"/>
        <add namespace="System.Web.Routing" />
      </namespaces>
    </pages>
  </system.web.webPages.razor>

  <appSettings>
    <add key="webpages:Enabled" value="false" />
  </appSettings>

  <system.web>
    <httpHandlers>
      <add path="*" verb="*" type="System.Web.HttpNotFoundHandler"/>
    </httpHandlers>

        <pages
        validateRequest="false"
        pageParserFilterType="System.Web.Mvc.ViewTypeParserFilter, System.Web.Mvc, Version=5.0.0.0, Culture=neutral, PublicKeyToken=31BF3856AD364E35"
        pageBaseType="Custom.MvcViewPage"
        userControlBaseType="Custom.MvcViewUserControl">
            <controls>
                <add assembly="System.Web.Mvc, Version=5.0.0.0, Culture=neutral, PublicKeyToken=31BF3856AD364E35" namespace="System.Web.Mvc" tagPrefix="mvc" />
            </controls>
        </pages>

Note: the app works when it runs, so this is a design-time only error.

Upvotes: 0

Views: 619

Answers (1)

p e p
p e p

Reputation: 6674

At the top of your view file, include:

@Imports System.Linq

Update:

On further inspection, System.Linq was properly included. However, I noticed that the Web.config had different values for the targetFramework attribute on the <httpRuntime> and <compilation> elements. Setting those to both be the same, namely 4.5.1, fixed the issue.

<httpRuntime targetFramework="4.5.1" />

Upvotes: 1

Related Questions