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