C# System.Collections.Generic.IEnumerable' 不包含 'ToList' 的任何定义

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/15812913/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-10 18:01:53  来源:igfitidea点击:

System.Collections.Generic.IEnumerable' does not contain any definition for 'ToList'

c#asp.net-mvc-3

提问by Dhwani

Here is the problem. I am getting IEnumerable from ViewPage and when I tried it to convert List it is showing me error like:

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

'System.Collections.Generic.IEnumerable<Pax_Detail>' does not contain a definition for 'ToList' and no extension method 'ToList' accepting a first argument of type 'System.Collections.Generic.IEnumerable<Pax_Detail>' could be found (are you missing a using directive or an assembly reference?)

System.Collections.Generic.IEnumerable<Pax_Detail>”不包含“ToList”的定义,并且找不到接受“ System.Collections.Generic.IEnumerable<Pax_Detail>”类型的第一个参数的扩展方法“ToList” (您是否缺少 using 指令或程序集引用?)

Here is my controller code:

这是我的控制器代码:

[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();
}

I have applied same logic on another controller. And it is working fine. I don't know what problem it has. I have already clean, rebuild project and also restarted my laptop(though it was needed).

我在另一个控制器上应用了相同的逻辑。它工作正常。我不知道它有什么问题。我已经清理,重建项目并重新启动了我的笔记本电脑(尽管需要)。

采纳答案by Anthony Sottile

Are you missing a using directive for System.Linq?

您是否缺少 using 指令System.Linq

http://msdn.microsoft.com/en-us/library/bb342261.aspx

http://msdn.microsoft.com/en-us/library/bb342261.aspx

回答by Adrian Godong

You're missing a reference to System.Linq.

您缺少对 System.Linq 的引用。

Add

添加

using System.Linq

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

访问当前代码文件上的 ToList() 函数。



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.Linqnamespace.

提供一些关于为什么需要这样做的信息,Enumerable.ToList<TSource>是一种扩展方法。扩展方法是在它所针对的原始类之外定义的。在这种情况下,扩展方法是在System.Linq命名空间上定义的。

回答by Daniel Hilgarth

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

添加 LINQ 的另一种方法是使用以下代码:

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

回答by Denis Besic

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

我缺少 System.Data.Entity dll 引用,问题已解决

回答by Myke Black

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.

就我而言,我从另一个使用 Automapper 的项目中复制了一些代码——我花了很长时间才弄明白。只需将 automapper nuget 包添加到项目中。