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. LINQ query - issue with nested collection

LINQ query - issue with nested collection

Scheduled Pinned Locked Moved LINQ
linqhelpquestioncsharpdatabase
2 Posts 2 Posters 3 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.
  • R Offline
    R Offline
    Rohde
    wrote on last edited by
    #1

    I'm trying to learn som LINQ stuff. I'm playing around with some collection stuff, but have hit a problem with a nested collection. I have a collection of movie critics. Each critic have a collection of movies they have reviewed. I'm trying to find the vote for a specific movie for a specific critic: I have this:

    var query = from critic in critics
    where
    critic.Name == "Hugo Bugo" && critic.MoviesReviewed.Any(m => m.Name == "Iron Man")
    select m.Vote;

    Obviously I cannot do a "select m.Vote" since I cannot access the m variable from the lambda expression. But how do I get to that vote?


    "When you have made evil the means of survival, do not expect men to remain good. Do not expect them to stay moral and lose their lives for the purpose of becoming the fodder of the immoral. Do not expect them to produce, when production is punished and looting rewarded. Do not ask, `Who is destroying the world?' You are."
    -Atlas Shrugged, Ayn Rand

    Richard DeemingR 1 Reply Last reply
    0
    • R Rohde

      I'm trying to learn som LINQ stuff. I'm playing around with some collection stuff, but have hit a problem with a nested collection. I have a collection of movie critics. Each critic have a collection of movies they have reviewed. I'm trying to find the vote for a specific movie for a specific critic: I have this:

      var query = from critic in critics
      where
      critic.Name == "Hugo Bugo" && critic.MoviesReviewed.Any(m => m.Name == "Iron Man")
      select m.Vote;

      Obviously I cannot do a "select m.Vote" since I cannot access the m variable from the lambda expression. But how do I get to that vote?


      "When you have made evil the means of survival, do not expect men to remain good. Do not expect them to stay moral and lose their lives for the purpose of becoming the fodder of the immoral. Do not expect them to produce, when production is punished and looting rewarded. Do not ask, `Who is destroying the world?' You are."
      -Atlas Shrugged, Ayn Rand

      Richard DeemingR Offline
      Richard DeemingR Offline
      Richard Deeming
      wrote on last edited by
      #2

      Try this:

      var query = from critic in critics
      where critic.Name == "Hugo Bugo"
      from movie in critic.MoviesReviewed
      where movie.Name == "Iron Man"
      select movie.Vote;


      "These people looked deep within my soul and assigned me a number based on the order in which I joined." - Homer

      "These people looked deep within my soul and assigned me a number based on the order in which I joined" - Homer

      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