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

Here is the problem. I get IEnumerable from ViewPage, and when I tried to convert List, it shows me an error:

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

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 applied the same logic to another controller. And it works fine. I do not know what's the problem. I already cleaned, rebuilt the project, and also restarted my laptop (although it was necessary).

+66
c # asp.net-mvc-3
Apr 04 '13 at
source share
5 answers

Are you missing the using directive for System.Linq ?

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

+189
Apr 04 '13 at
source share

You are missing a link to System.Linq.

Add

 using System.Linq 

to access the ToList () function in the current code file.




To give some information on why this is necessary, Enumerable.ToList<TSource> is an extension method. Extension methods are defined outside the source class that it targets. In this case, the extension method is defined in the System.Linq namespace.

+21
Apr 04 '13 at
source share

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

 List<Pax_Detail> paxList = new List<Pax_Detail>(pax); 
+7
Apr 04 '13 at 13:44 on
source share

I was missing a link to System.Data.Entity dll and the problem was resolved

+3
May 18 '15 at 9:46
source share

In my case, I copied some code from another project that Automapper used - it took me a long time to figure it out. It just needed to add the nugger automapper package to the project.

0
Jul 03 '19 at 10:46
source share



All Articles