Filter ObservalveCollection by single paramater
-
What I am doing is called lambda expressions. Its part of linq and i is basically a delegate variable that is the object youre selecting. So for yours maybe this will help. _uniquePartNumber = _collection.Where(i=>i.PropertyToFilter == selectedValue).Select(i=>i.PartNumber).Distinct(); This will select a distinct collection of partnumbers. You may need to cast it into an ObservableCollection.
-
What I am doing is called lambda expressions. Its part of linq and i is basically a delegate variable that is the object youre selecting. So for yours maybe this will help. _uniquePartNumber = _collection.Where(i=>i.PropertyToFilter == selectedValue).Select(i=>i.PartNumber).Distinct(); This will select a distinct collection of partnumbers. You may need to cast it into an ObservableCollection.
Back from my little mini-vaco and back at it. Before I left I did try something but no data came through. _uniqueFOV = (ObservableCollection<ObjectIDefined>)_collection.Select(i => i.FOV).Distinct(); I also tried this in a property for the UniqueFOV (i.e. the property that is bounded to) return (ObservableCollection<ObjectIDefined>)_collection.Select(i => i.FOV).Distinct(); Using the property no data comes through but there are no runtime issues. If I use the first method (which is called in my GetData() method), I get a runtime error for the casting. Specifically it says, "Unable to case object of type '<DistingIterator>d__7a'1[System.Int32]' to type 'System.Collections.ObjectModel.OberservalbelCollection'1[ObjectIDefined]'. As you ntoice I do not have the first filter (i.e. I am not using where). I do have drop downs that filter down but the first one (wether it be my FOV or the part number) is not filtered except for the uniqeness. Either way there are issues but I am not sure why.
-
Back from my little mini-vaco and back at it. Before I left I did try something but no data came through. _uniqueFOV = (ObservableCollection<ObjectIDefined>)_collection.Select(i => i.FOV).Distinct(); I also tried this in a property for the UniqueFOV (i.e. the property that is bounded to) return (ObservableCollection<ObjectIDefined>)_collection.Select(i => i.FOV).Distinct(); Using the property no data comes through but there are no runtime issues. If I use the first method (which is called in my GetData() method), I get a runtime error for the casting. Specifically it says, "Unable to case object of type '<DistingIterator>d__7a'1[System.Int32]' to type 'System.Collections.ObjectModel.OberservalbelCollection'1[ObjectIDefined]'. As you ntoice I do not have the first filter (i.e. I am not using where). I do have drop downs that filter down but the first one (wether it be my FOV or the part number) is not filtered except for the uniqeness. Either way there are issues but I am not sure why.
For the first method it would appear that FOV is an int?? So the results from that query would be IQueryable<int> so it would not cast to ObservableCollection<ObjectIDefined>.
-
For the first method it would appear that FOV is an int?? So the results from that query would be IQueryable<int> so it would not cast to ObservableCollection<ObjectIDefined>.
OK so it seems you are suggesting that the result is not my objects but one column from them (i.e. a list of one of their paramters). This would be fine also but not as I expected. However changing it I get, Unable to cast object of type '<DistinctIterator>d__7a`1[System.Int32]' to type 'System.Collections.ObjectModel.ObservableCollection`1[System.Int32]'.
-
OK so it seems you are suggesting that the result is not my objects but one column from them (i.e. a list of one of their paramters). This would be fine also but not as I expected. However changing it I get, Unable to cast object of type '<DistinctIterator>d__7a`1[System.Int32]' to type 'System.Collections.ObjectModel.ObservableCollection`1[System.Int32]'.
Are you still trying to cast it? Instead of
_uniqueFOV = (ObservableCollection<ObjectIDefined> )_collection.Select(i => i.FOV).Distinct();
Try this_uniqueFOV = (ObservableCollection<int> )_collection.Select(i => i.FOV).Distinct();
It just depends what the objects are you are selecting off of. What is the type used in the _collection variable? And what type of object are you expecting out? -
Are you still trying to cast it? Instead of
_uniqueFOV = (ObservableCollection<ObjectIDefined> )_collection.Select(i => i.FOV).Distinct();
Try this_uniqueFOV = (ObservableCollection<int> )_collection.Select(i => i.FOV).Distinct();
It just depends what the objects are you are selecting off of. What is the type used in the _collection variable? And what type of object are you expecting out?Yes that is what I had done. The objext is a custom object I made. It is called DefectItem and that is what is stored in _collection. I had assumed that would be what would come out if I used Distinct but by what you had said ealier (and by the error) it seems it is an int collection. Either way when I cast it I get the casting error I post previously. The difference of the casting seems to by the <DistinctIterator>
-
Yes that is what I had done. The objext is a custom object I made. It is called DefectItem and that is what is stored in _collection. I had assumed that would be what would come out if I used Distinct but by what you had said ealier (and by the error) it seems it is an int collection. Either way when I cast it I get the casting error I post previously. The difference of the casting seems to by the <DistinctIterator>
ok to get distincts of DefectItem out of _collection all you should need to do is _collection.Distinct() which will give you IQueryable<DefectItem> type collection which can be made into the ObserableCollection with all the distinct DefectItems in _collection.
-
ok to get distincts of DefectItem out of _collection all you should need to do is _collection.Distinct() which will give you IQueryable<DefectItem> type collection which can be made into the ObserableCollection with all the distinct DefectItems in _collection.
-
OK I think I have it figured out now. var unique = _defectItems.Select(i => i.FOV).Distinct(); foreach (int dItem in unique) _uniqueFOV.Add(dItem); Thank you for all your help!
I guess I still am having an issue. I had thought it was due to the binding and I could fix it but that doesn't make sence now. The issue is that nothing is returned on the property. I can break in the code when the filtered collection should be filled and it is filled properly but it is not being returned. However the non-filtered collection has a peroperty I can also bind to and similarily seems it returns nothing (if I break in the property) but it returns all elements (including duplicates). Any ideas here?
-
I guess I still am having an issue. I had thought it was due to the binding and I could fix it but that doesn't make sence now. The issue is that nothing is returned on the property. I can break in the code when the filtered collection should be filled and it is filled properly but it is not being returned. However the non-filtered collection has a peroperty I can also bind to and similarily seems it returns nothing (if I break in the property) but it returns all elements (including duplicates). Any ideas here?