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. C#
  4. Using IList.Contains<> to search nested members?

Using IList.Contains<> to search nested members?

Scheduled Pinned Locked Moved C#
questioncsharplinq
5 Posts 3 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.
  • J Offline
    J Offline
    JoeRip
    wrote on last edited by
    #1

    I cannot figure out the syntax for using the LINQ extensions on IList (and other interfaces) to search for values that are in nested objects. For instance, I have a List<foo>, where object foo contains two other objects, each of which have field "data". How do I use List.Contains<foo>("hello") to search for a foo object where foo.nestedobject.data = hello?

    M B 2 Replies Last reply
    0
    • J JoeRip

      I cannot figure out the syntax for using the LINQ extensions on IList (and other interfaces) to search for values that are in nested objects. For instance, I have a List<foo>, where object foo contains two other objects, each of which have field "data". How do I use List.Contains<foo>("hello") to search for a foo object where foo.nestedobject.data = hello?

      M Offline
      M Offline
      Matt T Heffron
      wrote on last edited by
      #2

      Why not ask in the LINQ forum where the LINQ guru's hang out?

      J 1 Reply Last reply
      0
      • M Matt T Heffron

        Why not ask in the LINQ forum where the LINQ guru's hang out?

        J Offline
        J Offline
        JoeRip
        wrote on last edited by
        #3

        Thanks. I asked here because it's not obvious that object.contains() is actually a LINQ extension. Sadly, you can't do what I want with object.contains(), which does not let you compare nested values directly. Turns out that what I need is

        foo.Any(f => f.bar.data == "hello")

        Which makes me a little sad, because

        foo.Contains(nested value I was looking for)

        would have been a lot simpler than having to break out my book and re-learn lambdas. Oh well, wave of the future :-)

        M 1 Reply Last reply
        0
        • J JoeRip

          Thanks. I asked here because it's not obvious that object.contains() is actually a LINQ extension. Sadly, you can't do what I want with object.contains(), which does not let you compare nested values directly. Turns out that what I need is

          foo.Any(f => f.bar.data == "hello")

          Which makes me a little sad, because

          foo.Contains(nested value I was looking for)

          would have been a lot simpler than having to break out my book and re-learn lambdas. Oh well, wave of the future :-)

          M Offline
          M Offline
          Matt T Heffron
          wrote on last edited by
          #4

          How could Contains() have any "idea" in which properties of foo to look for some value? For instance, should it look in all properties that are the same type as the value to find? What if it is nested arbitrarily deep? The only rational behavior is for the developer to be responsible to structure the query. Re: lambdas... they're not that hard! :)

          1 Reply Last reply
          0
          • J JoeRip

            I cannot figure out the syntax for using the LINQ extensions on IList (and other interfaces) to search for values that are in nested objects. For instance, I have a List<foo>, where object foo contains two other objects, each of which have field "data". How do I use List.Contains<foo>("hello") to search for a foo object where foo.nestedobject.data = hello?

            B Offline
            B Offline
            BobJanova
            wrote on last edited by
            #5

            How would Contains know to look inside its member items? You can either use Any with a lambda as you've discovered, or you can implement equality checking on the class Foo and do

            List.Contains(new Foo("hello"))

            ... if that makes any sense for the actual class you're using. I'd generally use the lambda, that makes it crystal clear what the test you're actually doing is.

            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