Kobojunkie
Kobojunkie

Reputation: 6545

System.Linq.IOrderedEnumerable does not contain a definition for 'ToList'

Now this one has me stumped and so I would appreciate some help please. I have this code tested in .NET 4.0 application, and it works fine. However, when I move it back to my 3.5 environment, I get the following error message when I build.

System.Linq.IOrderedEnumerable does not contain a definition for 'ToList' and the best extension method overload 'System.Linq.Enumerable.ToList(System.Collections.Generic.IEnumerable)' has some invalid arguments

And the code it complains of is the following

 List<String> PathValues = GetReportValues(settings.DirectoryDefinition.NameTokens.OrderBy(x => x.Index).ToList<Token>());

The signature for the GetReportValues method is as below

 private List<String> GetReportValues(List<Token> TokenList)
{
   List<String> PathValues = new List<String>();
/// code goes here
  return PathValues;
}

and for NameTokens, I have

   [XmlElement(Type = typeof(List<DirectoryPatternToken>))]
    public List<DirectoryPatternToken> NameTokens { get; set; }

The DirectoryDefinition.NameTokens object is a list containing objects derived from Token class. GetReportValues simply takes a list of tokens, of which DirectoryDefinition

 [Serializable]
public class DirectoryPatternToken : Token
{


}

As I explained earlier, this tests well in .NET 4.0. How can I get this working in 3.5 please? Thanks in advance

Upvotes: 0

Views: 3616

Answers (4)

Ananth
Ananth

Reputation: 10730

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.

Upvotes: 0

run2thesun
run2thesun

Reputation: 398

C# 4.0 implemented a feature called generic covariance:

http://msdn.microsoft.com/en-us/library/dd799517(v=vs.100).aspx

This allows a List< DirectoryPatternToken > to be treated as a List< Token > since DirectoryPatternToken derives from Token. Since C# 3.5 does not have this feature, you would have to use these lines to make your code compile:

List<String> PathValues = GetReportValues(NameTokens.OrderBy(x => x.index).ToList<DirectoryPatternToken>());

private static List<String> GetReportValues(List<DirectoryPatternToken> TokenList)

Upvotes: 0

vc 74
vc 74

Reputation: 38179

 List<DirectoryPatternToken> tokens = 
             settings.DirectoryDefinition.NameTokens.OrderBy(x => x.Index).ToList();
 List<string> pathValues = GetReportValues(tokens);

from which you can extract the string properties

Upvotes: 0

I4V
I4V

Reputation: 35363

Change .ToList<Token>() to .ToList<string>()

or simply write it as .ToList()

or do not use it at all, since your method already returns List<string>

Upvotes: 2

Related Questions