AsBindable() not compatible with Distinct()?

Oct 3, 2008 at 4:19 AM
I am having trouble with the following statement:

item.ItemsSource = (from p in photoCollection.AsBindable() select p.IsoSpeed).Distinct();

I am trying to set the items for a TreeViewItem. IsoSpeed is an int. With the previous statement I get the following error:

 The type 'int?' must be a reference type in order to use it as parameter 'TSource' in the generic type or method 'Bindable.Linq.Extensions.Distinct<TSource>(Bindable.Linq.IBindableCollection<TSource>)'

The following versions of the statement don't produce an error:
item.ItemsSource = (from p in photoCollection.AsBindable() select p.IsoSpeed);  /* no distinct */
item.ItemsSource = (from p in photoCollection.AsBindable() select p.IsoSpeed.ToString()).Distinct();  /*convert it to a string*/
item.ItemsSource = (from p in photoCollection select p.IsoSpeed).Distinct(); /* no bindable */

Any ideas on what the problem is? I am using Bindable 1.0 Beta 1. I want to get a list of the distinct IsoSpeeds from the PhotoCollection.

Oct 3, 2008 at 5:25 AM
Hi Luke,

Most iterators, such as Select, Where and Distinct, require the input type to be a reference type. I believe this was done to simplify some of the implementation issues. Your IsoSpeed is a nullable int, so it's not compatible with Distinct.

Taking another look at this I believe that limitation isn't necessary anymore, so I'll see what I can do about removing it in the next release.

Is the .ToString() solution a valid workaround for your scenario? You could probably do a Select after the distinct to conver the strings back to integers, but it's certainly not ideal.

Oct 6, 2008 at 9:49 PM
Thanks Paul! I ended up just using ToString and then just using a Binding Converter to change things around. I had to change it into a String eventually anyhow, so it wasn't that big a deal. I was trying to have as few converters as possible. Definitely a great program though!