Skip to content
  • Categories
  • Recent
  • Tags
  • Popular
  • World
  • Users
  • Groups
Skins
  • Light
  • Cerulean
  • Cosmo
  • Flatly
  • Journal
  • Litera
  • Lumen
  • Lux
  • Materia
  • Minty
  • Morph
  • Pulse
  • Sandstone
  • Simplex
  • Sketchy
  • Spacelab
  • United
  • Yeti
  • Zephyr
  • Dark
  • Cyborg
  • Darkly
  • Quartz
  • Slate
  • Solar
  • Superhero
  • Vapor

  • Default (No Skin)
  • No Skin
Collapse
Code Project
  1. Home
  2. General Programming
  3. LINQ
  4. Filter ObservalveCollection by single paramater

Filter ObservalveCollection by single paramater

Scheduled Pinned Locked Moved LINQ
csharplinqquestion
17 Posts 2 Posters 0 Views 1 Watching
  • Oldest to Newest
  • Newest to Oldest
  • Most Votes
Reply
  • Reply as topic
Log in to reply
This topic has been deleted. Only users with topic management privileges can see it.
  • L Lost User

    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.

    I Offline
    I Offline
    Ian McCaul
    wrote on last edited by
    #5

    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?

    L 1 Reply Last reply
    0
    • I Ian McCaul

      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?

      L Offline
      L Offline
      Lost User
      wrote on last edited by
      #6

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

      I 1 Reply Last reply
      0
      • L Lost User

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

        I Offline
        I Offline
        Ian McCaul
        wrote on last edited by
        #7

        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.

        L 2 Replies Last reply
        0
        • I Ian McCaul

          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.

          L Offline
          L Offline
          Lost User
          wrote on last edited by
          #8

          OH I see. I have not used lamdas before and had not realized that is what you were using. This is likely exactly what I am needing. Since my day/week is short though I will have to try it on Monday. Thank you for your help!

          1 Reply Last reply
          0
          • I Ian McCaul

            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.

            L Offline
            L Offline
            Lost User
            wrote on last edited by
            #9

            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.

            I 1 Reply Last reply
            0
            • L Lost User

              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.

              I Offline
              I Offline
              Ian McCaul
              wrote on last edited by
              #10

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

              L 1 Reply Last reply
              0
              • I Ian McCaul

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

                L Offline
                L Offline
                Lost User
                wrote on last edited by
                #11

                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]'.

                I 1 Reply Last reply
                0
                • L Lost User

                  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]'.

                  I Offline
                  I Offline
                  Ian McCaul
                  wrote on last edited by
                  #12

                  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?

                  L 1 Reply Last reply
                  0
                  • I Ian McCaul

                    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?

                    L Offline
                    L Offline
                    Lost User
                    wrote on last edited by
                    #13

                    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>

                    I 1 Reply Last reply
                    0
                    • L Lost User

                      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>

                      I Offline
                      I Offline
                      Ian McCaul
                      wrote on last edited by
                      #14

                      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.

                      L 1 Reply Last reply
                      0
                      • I Ian McCaul

                        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.

                        L Offline
                        L Offline
                        Lost User
                        wrote on last edited by
                        #15

                        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!

                        L 1 Reply Last reply
                        0
                        • L Lost User

                          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!

                          L Offline
                          L Offline
                          Lost User
                          wrote on last edited by
                          #16

                          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?

                          L 1 Reply Last reply
                          0
                          • L Lost User

                            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?

                            L Offline
                            L Offline
                            Lost User
                            wrote on last edited by
                            #17

                            Ok I figured this out now. I needed to replace my bound item on the UI as it was still thinking it was of type DefectItem.

                            1 Reply Last reply
                            0
                            Reply
                            • Reply as topic
                            Log in to reply
                            • Oldest to Newest
                            • Newest to Oldest
                            • Most Votes


                            • Login

                            • Don't have an account? Register

                            • Login or register to search.
                            • First post
                              Last post
                            0
                            • Categories
                            • Recent
                            • Tags
                            • Popular
                            • World
                            • Users
                            • Groups