1

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).

David Beavon
  • 1,141
  • 9
  • 16
  • I think your assumption about fields is correct. "The syntax (int sum, int count) indicates an **anonymous struct type with public fields** of the given names and types." (a quote from [roslyn issue on GitHub](https://github.com/dotnet/roslyn/issues/347)). I don't see any other solution except create a POCO class with *properties* and convert tuples to pocos in view models – ASh Mar 03 '17 at 07:28
  • 1
    Thanks for the confirmation. I had hoped I could use them for more programming scenarios. I actually wanted to bind the ValueTuples all the way up to my XAML views (even if it meant forgoing compiler services and using Item1/2/3 field names), but apparently that's not going to happen either. I suspect their purpose is only for transient/temporary work, comparable to anonymous types generated by linq queries. – David Beavon May 27 '17 at 15:13

0 Answers0