include nested object's property?
-
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
-
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
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
-
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
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!
-
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
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
} -
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
}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
-
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
from qTeam in teams select NickName = qTeam.NickName, Population = qTeam.City.Population