System.Array. does not contain a definition for "ToList"
Asked Answered
P

7

43

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?

Prewitt answered 4/4, 2011 at 12:22 Comment(1)
If someone is having the same issue, Take a deep look at Output window. Because Error window might be misleading sometimes.Corybantic
C
102

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

Centipoise answered 4/4, 2011 at 12:25 Comment(1)
Nice. I'm a little surprised you can't just right click on this in VisualStudio and find a "resolve ..." option.Sismondi
O
31

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
Orvas answered 20/11, 2015 at 21:24 Comment(2)
Same problem and solution in dotnet 4.5 ordinary unit test code. I have not checked why.Postfix
Correct. It has to be done when working with enums for instance. Getting a list of MyEnumType names as strings: System.Enum.GetValues(typeof(MyEnumType)).Cast<MyEnumType>().Select(v=>v.ToString()).ToList();Fortuitism
S
15

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

new List(array)

Srini answered 24/5, 2013 at 16:10 Comment(2)
That was a huge help! Don't forget the <T>, though: new List<T>(array);Suds
The code written by @paul is new List<string>(array). But given he didn't add it as 'code' block, the <string> part is rendered as Html tag. I tried to edit it, but it has been rejected :-|Debor
T
9

ToList() is an extension method. Maybe you're missing the

using System.Linq;
Trioxide answered 4/4, 2011 at 12:26 Comment(0)
B
6

This is simply because ArrayList does not expose a method named ToList.

See this MSDN page for a table view of the members available to you.

As explained by others, you may access this extension method by importing the Linq library:

using System.Linq;

Also, see this link for a custom implementation of such, should you desire to implement one.

Bolshevist answered 4/4, 2011 at 12:24 Comment(2)
You mean Array, not ArrayListCointon
@KonradRudolph - Well, now I do. ;) Could have sworn I read ArrayList in the title, but can't see any edits so I must be mistaken.Bolshevist
S
3

It's because the Enumerable extension methods aren't available.

You need to add "using System.Linq"

Strander answered 4/4, 2011 at 12:25 Comment(0)
E
0

This worked for me:

 var lst = ((IEnumerable<XElement>)doc.Element("cards").Elements("card")).ToList();

My initial result was simply a dynamic. The .Cast() method gave me the same error as ToList() but this did the trick.

Elaterid answered 30/7, 2019 at 13:23 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.