I'm a big fan of the new c# 7 ValueTuples. But here is one scenario where I'd like to use them, and it isn't working for me. If I create an ICollectionView against a collection of ValueTuples, it won't let me define sort descriptions. I guess this is pretty far off the beaten path for ValueTuple.
Code below:
static ObservableCollection<(string a, int b)> m_Static;
static void Main(string[] args)
{
m_Static = new ObservableCollection<(string a, int b)>( NewMethod());
ICollectionView ViewCollection = CollectionViewSource.GetDefaultView(m_Static);
ViewCollection.SortDescriptions.Clear();
ViewCollection.SortDescriptions.Add(new SortDescription("Item1", System.ComponentModel.ListSortDirection.Ascending));
ViewCollection.Refresh();
foreach ((string a, int b) LoopItem in ViewCollection)
{
System.Diagnostics.Debug.WriteLine("Message" + LoopItem.Item1);
}
}
private static List<(string a, int b)> NewMethod()
{
return new List<(string a, int b)>() { ("eTest", 2), ("aTest", 2) , ("cTest", 2) , ("bTest", 2) , ("zTest", 2) ,("fTest", 2) };
}
The output from the debugger says: BindingExpression path error: 'Item1' property not found on 'object' ''ValueTuple`2'
I suppose this is because the ValueTuple exposes no properties (ie. "Item1"..."Item#" are fields not properties.)
Obviously I could rebuild my lists using a linq query whenever I want to sort them differently, but I was hoping that I would be able to use a more abstract API's (ICollectionView, CollectionViewSource, and so on) which allows different views against an existing list.
(Here is a linq that works to build a new list in a desired order: var ListThem = from (string a, int b) x in m_Static orderby x.a select x;)
If anyone has been down this road and found a way to use ICollectionView features against ValueTuples, please let me know.
I think ValueTuples are a really powerful feature, and they sometimes appear deceptively simple. The compiler lets you go to some strange places when using them (if you aren't careful).