Filter ObservalveCollection by single paramater
-
I am not sure if this can be done easily but I figured I would ask rather than write a double loop to filter out anything not unique. I have an ObservableCollection of objects I defined. They have an 'int' paramater called FOV. I would like a property that returns an ObservableCollection<OjectIDefined> from my raw data that has filtered out all non-unique elements ( i.e. no duplicates). Can this be done without looping through the collection multiple times? i.e. gets the unique data in a LINQ statement and then fills a filtered Collection in one loop.
-
I am not sure if this can be done easily but I figured I would ask rather than write a double loop to filter out anything not unique. I have an ObservableCollection of objects I defined. They have an 'int' paramater called FOV. I would like a property that returns an ObservableCollection<OjectIDefined> from my raw data that has filtered out all non-unique elements ( i.e. no duplicates). Can this be done without looping through the collection multiple times? i.e. gets the unique data in a LINQ statement and then fills a filtered Collection in one loop.
Digging around maybe I should convert to a List and create a Comparer.. http://msdn.microsoft.com/en-us/library/bb338049.aspx[^] However, I do not understand the GetHashCode... If this is the proper way could someone explain it. Otherwise if anyone knows a way to do this using a simple LINQ statement that would be best.
-
Digging around maybe I should convert to a List and create a Comparer.. http://msdn.microsoft.com/en-us/library/bb338049.aspx[^] However, I do not understand the GetHashCode... If this is the proper way could someone explain it. Otherwise if anyone knows a way to do this using a simple LINQ statement that would be best.
I think you could just use ObservableCollection.Distinct(). This will filter out all non unique values and give you just 1 of each element in the collection. Is this what you are looking for? P.S. Distinct() is an extension method from linq. You would have to include System.Linq into your code file.
-
I think you could just use ObservableCollection.Distinct(). This will filter out all non unique values and give you just 1 of each element in the collection. Is this what you are looking for? P.S. Distinct() is an extension method from linq. You would have to include System.Linq into your code file.
The items are not the same though. They have one parameter that is the same. The reason I need to do it is because on the UI side users need multiple drop down Listboxes for multiple paramaters to filter the data by. These parameters are ofcourse not unique identifiers so when the listbox is populated it is populated with all duplicates also. I would like to create filtered Collections that populate(bound to) the listbox so the user does not see duplicates. Then when they select an item(s) I use it to get the requested data.
-
The items are not the same though. They have one parameter that is the same. The reason I need to do it is because on the UI side users need multiple drop down Listboxes for multiple paramaters to filter the data by. These parameters are ofcourse not unique identifiers so when the listbox is populated it is populated with all duplicates also. I would like to create filtered Collections that populate(bound to) the listbox so the user does not see duplicates. Then when they select an item(s) I use it to get the requested data.
Ok I think I am getting the picture... So you have a collection of objects that continually get more filtered based on multiple drop downs? In this case you can select the specific piece of the object you want to bind to and then do distinct... i.e. Collection.Select(i=i.Name).Distinct(); // This will select all the Names from the collection so it should be an array of strings without duplicates Then when one is selected you could do this Collection.Where(i=>i.Name == selectedName).Select(i=>i.Title).Distinct() // This will select the objects where the name property equals the selectedName and then select their titles without duplicates. Does this help or am I off base still?
-
Ok I think I am getting the picture... So you have a collection of objects that continually get more filtered based on multiple drop downs? In this case you can select the specific piece of the object you want to bind to and then do distinct... i.e. Collection.Select(i=i.Name).Distinct(); // This will select all the Names from the collection so it should be an array of strings without duplicates Then when one is selected you could do this Collection.Where(i=>i.Name == selectedName).Select(i=>i.Title).Distinct() // This will select the objects where the name property equals the selectedName and then select their titles without duplicates. Does this help or am I off base still?
Yes you have the write idea but maybe I am missing something in your explanation of how to do it. So I have my collection that gets filled. Lets call it _collection Ite can be bound to by calling my property that is
public ObservableCollectionObservableCollection { get { return \_collection }
I am picturing a second property that is as such
public ObservableCollection UniquePartNumber
{
get { return _uniquePartNumber; }
}So the question is how does _uniquePartNumber get populated? You have Collection.Select(i=i.Name).Distinct() I am not sure what 'i' actually is. Is this the item in the LINQ statement? Thank you for your help. Just in case you don't respond till this afternoon I am taking a long weekend starting in 20 min. So if I don't respond till Monday that is why ;)
-
Yes you have the write idea but maybe I am missing something in your explanation of how to do it. So I have my collection that gets filled. Lets call it _collection Ite can be bound to by calling my property that is
public ObservableCollectionObservableCollection { get { return \_collection }
I am picturing a second property that is as such
public ObservableCollection UniquePartNumber
{
get { return _uniquePartNumber; }
}So the question is how does _uniquePartNumber get populated? You have Collection.Select(i=i.Name).Distinct() I am not sure what 'i' actually is. Is this the item in the LINQ statement? Thank you for your help. Just in case you don't respond till this afternoon I am taking a long weekend starting in 20 min. So if I don't respond till Monday that is why ;)
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.
-
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?