Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

System.Array. does not contain a definition for "ToList"

I'm getting the above error on the ToList() line of the code below

if (emailReplyTo != null)
{
  System.Collections.Generic.List<String> replyto
    = emailReplyTo
    // Strip uneccessary spaces
    .Replace(", ", ",")
    .Split(',')
    .ToList();

  request.WithReplyToAddresses(emailReplyTo);
}

I have included using System.Collections; at the top of my file. The target framework is 3.5, so why is this causing an error?

like image 594
fearoffours Avatar asked Apr 04 '11 12:04

fearoffours


3 Answers

The ToList method you are looking for is an extension method. Try adding this using directive to the top of your file:

using System.Linq;

By adding this using directive you are indicating to the compiler that any extension methods in that namespace should be imported. It's kind of a shame that there isn't more help from Visual Studio around importing extension methods (ReSharper does this rather nicely).

like image 146
Andrew Hare Avatar answered Oct 08 '22 04:10

Andrew Hare


In case someone stumbles on this questions after googling...

I had the exact same problem in Razor views and adding using System.Linq at the top didn't help.

What did help is calling .Cast() before using Linq extension methods:

myArrayVariable.Cast<SomeClass>().ToList() //ok, NOW ToList works fine
like image 45
Alex from Jitbit Avatar answered Oct 08 '22 05:10

Alex from Jitbit


You can also do this without .toList, saves including an entire library for no real reason.

new List(array)

like image 43
Paul Hutchinson Avatar answered Oct 08 '22 04:10

Paul Hutchinson