System.Collections.Generic.IEnumable’不包含“ ToList”的任何定义

这就是问题所在。我正在从 ViewPage 获取 IEnumable,当我尝试转换 List 时,它显示了如下错误:

System.Collections.Generic.IEnumerable<Pax_Detail>”不包含 “ ToList”的定义,没有扩展方法“ ToList”接受 类型的第一个参数 可以找到“ System.Collections.Generic.IEnumerable<Pax_Detail>” (是否缺少 using 指令或汇编引用?)

这是我的控制器代码:

[HttpPost]
public ActionResult Edit_Booking(Booking model, IEnumerable<Pax_Detail> pax)
{
List<Pax_Detail> paxList = new List<Pax_Detail>();
paxList = pax.ToList(); //getting error here
BookingDL.Update_Booking(model, paxList);
return View();
}

我已经在另一个控制器上应用了相同的逻辑。而且效果很好。我不知道有什么问题。我已经清理,重建项目,并重新启动我的笔记本电脑(虽然它是必要的)。

155659 次浏览

You're missing a reference to System.Linq.

Add

using System.Linq

to get access to the ToList() function on the current code file.


To give a little bit of information over why this is necessary, Enumerable.ToList<TSource> is an extension method. Extension methods are defined outside the original class that it targets. In this case, the extension method is defined on System.Linq namespace.

An alternative to adding LINQ would be to use this code instead:

List<Pax_Detail> paxList = new List<Pax_Detail>(pax);

I was missing System.Data.Entity dll reference and problem was solved

In my case, I had copied some code from another project that was using Automapper - took me ages to work that one out. Just had to add automapper nuget package to project.