System.Array. does not contain a definition for “ToList”

后端 未结 4 2012
被撕碎了的回忆
被撕碎了的回忆 2021-02-11 14:38

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

if (emailReplyTo != null)
{
  System.Collections.Generic.List replyto
    = ema         


        
相关标签:
4条回答
  • 2021-02-11 15:04

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

    using System.Linq;
    
    0 讨论(0)
  • 2021-02-11 15:11

    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
    
    0 讨论(0)
  • 2021-02-11 15:16

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

    new List(array)

    0 讨论(0)
  • 2021-02-11 15:21

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

    0 讨论(0)
提交回复
热议问题