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. include nested object's property?

include nested object's property?

Scheduled Pinned Locked Moved LINQ
questioncsharplinqcollaborationperformance
6 Posts 5 Posters 16 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
    johnnys appleseed
    wrote on last edited by
    #1

    Say I have these in-memory objects: Team object. It has 2 properties: Nickname (string) City (another object) City object. It has 1 property: Population (int) Using LINQ, how do I select the Nickname and Population into the same result? I tend to isolate the City object, then join to it using Population. Isn't there a better way? Marty

    M E D 3 Replies Last reply
    0
    • J johnnys appleseed

      Say I have these in-memory objects: Team object. It has 2 properties: Nickname (string) City (another object) City object. It has 1 property: Population (int) Using LINQ, how do I select the Nickname and Population into the same result? I tend to isolate the City object, then join to it using Population. Isn't there a better way? Marty

      M Offline
      M Offline
      MidwestLimey
      wrote on last edited by
      #2

      I'm assuming you have a list of Teams, but given:

          public class Team
          {
              public string Nickname { get; set; }
              public City City { get; set; }
              // yada
      
          }
          public class City
          {
              public int Population { get; set; }
              // yada
          }
      

      I suspect you're looking for something like ...

          IEnumerable teams = getMeSomeTeamsDagnamit();
          var result =
              from t in teams
              select new { t.Nickname, t.City.Population };
      

      I'd suggest you read up on Anonymous Types [^] if you're not familiar.

      062142174041062102

      J 1 Reply Last reply
      0
      • M MidwestLimey

        I'm assuming you have a list of Teams, but given:

            public class Team
            {
                public string Nickname { get; set; }
                public City City { get; set; }
                // yada
        
            }
            public class City
            {
                public int Population { get; set; }
                // yada
            }
        

        I suspect you're looking for something like ...

            IEnumerable teams = getMeSomeTeamsDagnamit();
            var result =
                from t in teams
                select new { t.Nickname, t.City.Population };
        

        I'd suggest you read up on Anonymous Types [^] if you're not familiar.

        062142174041062102

        J Offline
        J Offline
        johnnys appleseed
        wrote on last edited by
        #3

        I realize now that my original sample code was too simplistic. In reality, the "Team" object was an (incorrect) projection from a "groupby", where the "City" wasn't available. We figured it out. Thanks for the info!

        1 Reply Last reply
        0
        • J johnnys appleseed

          Say I have these in-memory objects: Team object. It has 2 properties: Nickname (string) City (another object) City object. It has 1 property: Population (int) Using LINQ, how do I select the Nickname and Population into the same result? I tend to isolate the City object, then join to it using Population. Isn't there a better way? Marty

          E Offline
          E Offline
          ewohlman
          wrote on last edited by
          #4

          Shouldn't be difficult, just use the . operator to drill into the City property.

          var teams = CollectionOfTeams();
          var namePops = from team in teams
          select new { Name = team.Nickname,
          Pop = team.City.Population };

          foreach(var teamPop in namePops)
          {
          //Do whatever
          }

          G 1 Reply Last reply
          0
          • E ewohlman

            Shouldn't be difficult, just use the . operator to drill into the City property.

            var teams = CollectionOfTeams();
            var namePops = from team in teams
            select new { Name = team.Nickname,
            Pop = team.City.Population };

            foreach(var teamPop in namePops)
            {
            //Do whatever
            }

            G Offline
            G Offline
            Gabriel Szabo
            wrote on last edited by
            #5

            Wow! This is the proof that time travel is actually possible ;). Now seriously, the above post is crosslinked into the LINQ discussion named "data check in query". Check here: http://www.codeproject.com/Forums/1004117/LINQ.aspx[^]

            Gabriel Szabo

            1 Reply Last reply
            0
            • J johnnys appleseed

              Say I have these in-memory objects: Team object. It has 2 properties: Nickname (string) City (another object) City object. It has 1 property: Population (int) Using LINQ, how do I select the Nickname and Population into the same result? I tend to isolate the City object, then join to it using Population. Isn't there a better way? Marty

              D Offline
              D Offline
              DavidSherwood
              wrote on last edited by
              #6

              from qTeam in teams select NickName = qTeam.NickName, Population = qTeam.City.Population

              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