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

C#asp.net Mvc-3

C# Problem Overview


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

> '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?)

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

C# Solutions


Solution 1 - C#

Are you missing a using directive for System.Linq?

https://docs.microsoft.com/en-us/dotnet/api/system.linq.enumerable.tolist

Solution 2 - C#

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.

Solution 3 - C#

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

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

Solution 4 - C#

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

Solution 5 - C#

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.

Attributions

All content for this solution is sourced from the original question on Stackoverflow.

The content on this page is licensed under the Attribution-ShareAlike 4.0 International (CC BY-SA 4.0) license.

Content TypeOriginal AuthorOriginal Content on Stackoverflow
QuestionDhwaniView Question on Stackoverflow
Solution 1 - C#Anthony SottileView Answer on Stackoverflow
Solution 2 - C#Adrian GodongView Answer on Stackoverflow
Solution 3 - C#Daniel HilgarthView Answer on Stackoverflow
Solution 4 - C#Denis BesicView Answer on Stackoverflow
Solution 5 - C#Myke BlackView Answer on Stackoverflow