Cannot implicitly convert type 'System.Collections.Generic.IEnumerable GroupingLayer to 'System.Collections.IList'

StackOverflow https://stackoverflow.com/questions/21988295

Question

I have this code :

var selected = from c in myList group c by c.MainTitle into n select new GroupingLayer<string, MyObject>(n);
            longListSelector.ItemsSource = selected; //Error here


public class GroupingLayer<TKey, TElement> : IGrouping<TKey, TElement>
        {

            private readonly IGrouping<TKey, TElement> grouping;

            public GroupingLayer(IGrouping<TKey, TElement> unit)
            {
                grouping = unit;
            }

            public TKey Key
            {
                get { return grouping.Key; }
            }

            public IEnumerator<TElement> GetEnumerator()
            {
                return grouping.GetEnumerator();
            }

            System.Collections.IEnumerator System.Collections.IEnumerable.GetEnumerator()
            {
                return grouping.GetEnumerator();
            }
        }

What does its trying to tell me ?, how to solve this ?

I am Working on windows Phone 8 application.

Était-ce utile?

La solution

It's telling you that longListSelector.ItemsSource is of type IList, but the value stored in selected is an IEnumerable, and you can't assign one to the other.

Try calling ToList():

var selected = (from c in myList
                group c by c.MainTitle into n
                select new GroupingLayer<string, MyObject>(n)).ToList();

longListSelector.ItemsSource = selected;
Licencié sous: CC-BY-SA avec attribution
Non affilié à StackOverflow
scroll top