Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

System.Collections.Generic.List does not contain a definition for 'Select' [duplicate]

Tags:

c#

linq

This error is happening in many of the files in my "Views" folder:

'System.Collection.GenericList' does not contain a definition for 'Select' accepting a first argument of type 'System.Collections.GenericList' could be found (are you missing a using directive or an assembly reference?)

I've tried adding a bunch of "using System..." and other basic libraries near the top of the file but adding those do not seem to help any.

This is where the error occurs for me is in the line that starts with .BindTo(Model.Users.Select(o => o.UserName)):

Any help would be greatly appreciated. Thanks!

 <div id="editRolesContainer" class="detailContainer detailContainer4">
    <header class="sectionheader"> Add Roles </header>
    <ul id = "AdminSelectUserContainer" >
       <li>
          <ul style="padding: 0 0 0 5px">
             <li>Select User : </li>
             <li>
                @using (Html.BeginForm("srch_GetUserRoles", "Admin",
                   new { view = "Users_Roles" }, FormMethod.Post,
                   new { name = "srch_GetUserRoles" }))
                {
                   @(Html.Telerik().AutoComplete()
                          .Name("acx_SelectUser")
                          .BindTo(Model.Users.Select(o => o.UserName))
                              .HtmlAttributes(new { type "submit"   })
                          .HtmlAttributes(new { @class = "SearchBox"})
                          .AutoFill(true)
                          .Filterable((filtering =>
                              {
                                 filtering.FilterMode(AutoCompleteFilterMode.Contains);
                              }))
                  )
                }
             </li>
            </ul>
           ...
           ...
           </div>
like image 721
Ogreintel Avatar asked Jun 30 '14 18:06

Ogreintel


Video Answer


3 Answers

Just add this namespace,

using System.Linq; 
like image 75
Sajeetharan Avatar answered Sep 24 '22 05:09

Sajeetharan


You need to have the System.Linq namespace included in your view since Select is an extension method. You have a couple of options on how to do this:

Add @using System.Linq to the top of your cshtml file.

If you find that you will be using this namespace often in many of your views, you can do this for all views by modifying the web.config inside of your Views folder (not the one at the root). You should see a pages/namespace XML element, create a new add child that adds System.Linq. Here is an example:

<configuration>
    <system.web.webPages.razor>
        <pages>
            <namespaces>
                <add namespace="System.Linq" />
            </namespaces>
        </pages>
    </system.web.webPages.razor>
</configuration>
like image 45
vcsjones Avatar answered Sep 24 '22 05:09

vcsjones


I had this issue , When calling Generic.List like:

mylist.Select( selectFunc )

Where selectFunc is defined as Expression<Func<T, List<string>>>. Simply changed "mylist" to be a IQuerable instead of List then it allowed me to use .Select.

like image 36
John Avatar answered Sep 22 '22 05:09

John