Returning proper type after using OrderBy()

Question!

I have a collection class that inherits from List<>. I've set up a function to sort the list by a certain property like so:

public PlaylistCollection SortByName(IEnumerable<Playlist> playlists)
{
    return (PlaylistCollection)playlists.OrderBy(p => p.Name);
}

When I try to use the sorted results in my code like this:

artistSource.Playlists = (PlaylistCollection)new List<Playlist>(artistSource.Playlists.SortByName(artistSource.Playlists));

I get the error:

Unable to cast object of type 'System.Linq.OrderedEnumerable`2[...Playlist,System.String]'
 to type '...PlaylistCollection'."

This is moderately frustrating considering VS told me that an explicit conversion exists, so I added the above cast.

How do I properly cast from IEnumerable<> to my collection?



Answers

It seems you're looking less for how to cast the result and more how to stuff it into a List<>. I was looking for a simple way to do the same thing and found the following solution:

artistSource.PlayList = playlists.OrderBy(p => p.Name).ToList<PlayList>();

The ToList extension method is available in .NET 3.5. The following links contain additional information:

Enumerable.ToList Method (MSDN)

Sorting a List<FileInfo> by creation date C#

By : Phil


Yet another answer. I tried the above, which works in WPF (AlternationCount solution), but I needed code for Silverlight, so I did the following. This is more elegant than the other brute force method.

<UserControl xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
  xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml" 
  xmlns:local="clr-namespace:RowNumber" x:Name="userControl"
  x:Class="RowNumber.MainPage" Width="640" Height="480">
<Grid x:Name="LayoutRoot" Background="White">
  <ListBox ItemsSource="{Binding Test, ElementName=userControl}">
     <ListBox.Resources>
        <local:ListItemIndexConverter x:Key="IndexConverter" />
     </ListBox.Resources>
     <ListBox.ItemTemplate>
        <DataTemplate>
           <StackPanel Orientation="Horizontal">
              <TextBlock Width="30"
                    Text="{Binding RelativeSource={RelativeSource AncestorType=ListBoxItem}, Converter={StaticResource IndexConverter}}" />
              <TextBlock Text="{Binding}" />
           </StackPanel>
        </DataTemplate>
     </ListBox.ItemTemplate>
  </ListBox>
</Grid>
</UserControl>

And behind

  using System;
  using System.Collections.Generic;
  using System.Globalization;
  using System.Linq;
  using System.Windows.Controls;
  using System.Windows.Controls.Primitives;
  using System.Windows.Data;

  namespace RowNumber
  {
     public class ListItemIndexConverter : IValueConverter
     {
        // Value should be ListBoxItem that contains the current record. RelativeSource={RelativeSource AncestorType=ListBoxItem}
        public object Convert(object value, Type targetType, object parameter, CultureInfo culture)
        {
           var lbi = (ListBoxItem)value;
           var listBox = lbi.GetVisualAncestors().OfType<ListBox>().First();
           var index = listBox.ItemContainerGenerator.IndexFromContainer(lbi);
           // One based. Remove +1 for Zero based array.
           return index + 1;
        }
        public object ConvertBack(object value, Type targetType, object parameter, CultureInfo culture) { throw new NotImplementedException(); }
     }
     public partial class MainPage : UserControl
     {
        public MainPage()
        {
           // Required to initialize variables
           InitializeComponent();
        }
        public List<string> Test { get { return new[] { "Foo", "Bar", "Baz" }.ToList(); } }
     }
  }

This is newly available in Silverlight 5 with the introduction of RelativeSource binding.



The idea in David Brown's link was to use a value converter which worked. Below is a full working sample. The list box has row numbers and can be sorted on both name and age.

XAML:

<Window x:Class="NumberedListBox.Window1"
    xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
    xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
    xmlns:local="clr-namespace:NumberedListBox"
    Height="300" Width="300">

    <Window.Resources>

        <local:RowNumberConverter x:Key="RowNumberConverter" />

        <CollectionViewSource x:Key="sortedPersonList" Source="{Binding Path=Persons}" />

    </Window.Resources>

    <Grid>
        <Grid.RowDefinitions>
            <RowDefinition />
            <RowDefinition Height="Auto"/>
        </Grid.RowDefinitions>
        <Grid.ColumnDefinitions>
            <ColumnDefinition />
            <ColumnDefinition />
        </Grid.ColumnDefinitions>
        <ListBox 
            Grid.Row="0" Grid.Column="0" Grid.ColumnSpan="2"
            ItemsSource="{Binding Source={StaticResource sortedPersonList}}" 
            HorizontalContentAlignment="Stretch">
            <ListBox.ItemTemplate>
                <DataTemplate>
                    <StackPanel Orientation="Horizontal">
                        <TextBlock 
                            Text="{Binding Converter={StaticResource RowNumberConverter}, ConverterParameter={StaticResource sortedPersonList}}" 
                            Margin="5" />
                        <TextBlock Text="{Binding Path=Name}" Margin="5" />
                        <TextBlock Text="{Binding Path=Age}" Margin="5" />
                    </StackPanel>
                </DataTemplate>
            </ListBox.ItemTemplate>
        </ListBox>
        <Button Grid.Row="1" Grid.Column="0" Content="Name" Tag="Name" Click="SortButton_Click" />
        <Button Grid.Row="1" Grid.Column="1" Content="Age" Tag="Age" Click="SortButton_Click" />
    </Grid>
</Window>

Code behind:

using System;
using System.Collections.ObjectModel;
using System.Windows.Data;
using System.Windows;
using System.ComponentModel;
using System.Windows.Controls;

namespace NumberedListBox
{
    public partial class Window1 : Window
    {
        public Window1()
        {
            InitializeComponent();

            Persons = new ObservableCollection<Person>();
            Persons.Add(new Person() { Name = "Sally", Age = 34 });
            Persons.Add(new Person() { Name = "Bob", Age = 18 });
            Persons.Add(new Person() { Name = "Joe", Age = 72 });
            Persons.Add(new Person() { Name = "Mary", Age = 12 });

            CollectionViewSource view = FindResource("sortedPersonList") as CollectionViewSource;
            view.SortDescriptions.Add(new SortDescription("Name", ListSortDirection.Ascending));

            DataContext = this;
        }

        public ObservableCollection<Person> Persons { get; private set; }

        private void SortButton_Click(object sender, RoutedEventArgs e)
        {
            Button button = sender as Button;
            string sortProperty = button.Tag as string;
            CollectionViewSource view = FindResource("sortedPersonList") as CollectionViewSource;
            view.SortDescriptions.Clear();
            view.SortDescriptions.Add(new SortDescription(sortProperty, ListSortDirection.Ascending));

            view.View.Refresh();
        }
    }

    public class Person
    {
        public string Name { get; set; }
        public int Age { get; set; }
    }
}

Value converter:

using System;
using System.Windows.Data;

namespace NumberedListBox
{
    public class RowNumberConverter : IValueConverter
    {
        public object Convert(object value, Type targetType, object parameter, System.Globalization.CultureInfo culture)
        {
            CollectionViewSource collectionViewSource = parameter as CollectionViewSource;

            int counter = 1;
            foreach (object item in collectionViewSource.View)
            {
                if (item == value)
                {
                    return counter.ToString();
                }
                counter++;
            }
            return string.Empty;
        }

        public object ConvertBack(object value, Type targetType, object parameter, System.Globalization.CultureInfo culture)
        {
            throw new NotImplementedException();
        }
    }
}


This video can help you solving your question :)
By: admin