I have an ObservableCollection like the following-
private ObservableCollection<KeyedList<int, Anime>> _grp;
public ObservableCollection<KeyedList<int, Anime>> GroupedAnimeByGenre
{
get
{
return _grp;
}
set
{
_grp = value;
RaisePropertyChanged("GroupedAnimeByGenre");
}
}
I am using this to populate a LongListSelector with grouping. The KeyedList is implemented like this-
public class KeyedList<TKey, TItem> : List<TItem>
{
public TKey Key { protected set; get; }
public KeyedList(TKey key, IEnumerable<TItem> items)
: base(items)
{
Key = key;
}
public KeyedList(IGrouping<TKey, TItem> grouping)
: base(grouping)
{
Key = grouping.Key;
}
}
I have the following code to feed the ObservableCollection. Keep in mind AnimeList2 is a temporary Collection.
var groupFinale = AnimeList2.GroupBy(txt => txt.id).Where(grouping => grouping.Count() > 1).ToObservableCollection();
GroupedAnimeByGenre = groupFinale ;
But I am unable to convert/use groupFinale with GroupedAnimeByGenre. I am missing the extension method part as I am not well aware of the syntax. Please help
If you remove the
ToObservableCollection()call and take just that partyou'll see that the type of
groupFinaleisIEnumerable<IGrouping<int, Anime>>. Hence applyingToObservableCollection()will result inObservableCollection<IGrouping<int, Anime>>. However, the type of theGroupedAnimeByGenreisObservableCollection<KeyedList<int, Anime>>. So you need to convertIEnumerable<IGrouping<int, Anime>>toIEnumerable<KeyedList<int, Anime>>which in LINQ is performed by the Select method.Shortly, you can use something like this
You can make such conversion easier by providing an extension method (similar to BCL provided
ToArray()/ToList()) that will allow skipping the type arguments like thisThen you can use simply