System.Collections.Generic.List 不包含“ Select”的定义

这个错误发生在我的“视图”文件夹中的许多文件中:

“ System.Collection.GenericList”不包含 接受类型为 可以找到“ System.Collections.GenericList”(您是否 缺少 using 指令或汇编引用?)

我试过在文件顶部附近添加一些“ using System...”和其他基本库,但添加这些似乎没有任何帮助。

对我来说,错误发生在以 .BindTo(Model.Users.Select(o => o.UserName))开头的行中:

如果您能帮忙,我将不胜感激。谢谢!

 <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>
131258 次浏览

Just add this namespace,

using System.Linq;

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>

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.

This question's bit old, but, there's a tricky scenario which also leads to this error:

In controller:

ViewBag.id = //id from querystring
List<string> = GrabDataFromDBByID(ViewBag.id).Select(a=>a.ToString());

The above code will lead to an error in this part: .Select(a=>a.ToString()) because of the below reason: You're passing a ViewBag.id to a method which in compiler, it doesn't know the type, so there might be several methods with the same name and different parameters let's say:

GrabDataFromDBByID(string)
GrabDataFromDBByID(int)
GrabDataFromDBByID(whateverType)

So to prevent this case, either explicitly cast the ViewBag or create another variable storing it.