views:

67

answers:

1

Working with Sitecore and Linq extensions.

I am trying to convert to from an item array to the list using the following piece of code:

Item variationsFolder = masterDB.SelectSingleItem(VariationsFolderID.ToString());
List<Item> variationList = variationsFolder.GetChildren().ToList<Item>();

However I keep getting this error whenever I try to build:

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

I have the following usings:

using System.Linq;
using System.Xml.Linq;

Am referencing:

System.Core

I've just copied this code from another location, so it should work fine, can only think that there is something simple (like a reference or something that I am missing).

+2  A: 

I don't have any experience with SiteCore, but does ChildList implement IEnumerable rather than IEnumerable<T> perhaps?

If so, try this:

List<Item> variationList = variationsFolder.GetChildren()
                                           .Cast<Item>()
                                           .ToList();

Basically Cast<T> converts an IEnumerable to IEnumerable<T> by casting each element.

Jon Skeet
Notice that this is fixed in version 6.2.Then you can use ToList() right away.
Alex de Groot