From: shapper on 21 Jan 2010 20:10 Hello, I have a list of objects: IList<Center> centers. Each Center has two properties: Place and Name. I need to create a loops through the distinct Places in all centers and for each Place displays the centers names. My problem is how here: foreach (Place p in centers. ??? Thanks, Miguel
From: Arne Vajhøj on 21 Jan 2010 20:33 On 21-01-2010 20:10, shapper wrote: > I have a list of objects: IList<Center> centers. Each Center has two > properties: Place and Name. > > I need to create a loops through the distinct Places in all centers > and for each Place displays the centers names. > > My problem is how here: > > foreach (Place p in centers. ??? Untested: foreach (Place p in centers.Select(c => c.Place).Distinct()) Arne
From: Family Tree Mike on 21 Jan 2010 20:55 On 1/21/2010 8:33 PM, Arne Vajh�j wrote: > On 21-01-2010 20:10, shapper wrote: >> I have a list of objects: IList<Center> centers. Each Center has two >> properties: Place and Name. >> >> I need to create a loops through the distinct Places in all centers >> and for each Place displays the centers names. >> >> My problem is how here: >> >> foreach (Place p in centers. ??? > > Untested: > > foreach (Place p in centers.Select(c => c.Place).Distinct()) > > Arne > Just adding to what Arne started: (sorry about the formatting). List<Center> centers = new List<Center>(); centers.Add(new Center("Tennessee", "Knoxville")); centers.Add(new Center("Arizona", "Tuscon")); centers.Add(new Center("New Mexico", "Albuquerque")); centers.Add(new Center("Arizona", "Phoenix")); foreach (string UniquePlace in centers.OrderBy(c => c.Place).Select(c => c.Place).Distinct()) { Console.WriteLine(UniquePlace); foreach (string UniqueCenterInPlace in centers.OrderBy(c => c.Name).Where(c => (c.Place == UniquePlace)).Select(c => c.Name)) Console.WriteLine("\t{0}", UniqueCenterInPlace); } -- Mike
From: kndg on 21 Jan 2010 21:20 On 1/22/2010 9:10 AM, shapper wrote: > Hello, > > I have a list of objects: IList<Center> centers. Each Center has two > properties: Place and Name. > > I need to create a loops through the distinct Places in all centers > and for each Place displays the centers names. > > My problem is how here: > > foreach (Place p in centers. ??? > > Thanks, > Miguel I take Mike data as example: public class Center { public string Place { get; private set; } public string Name { get; private set; } public Center(string place, string name) { Place = place; Name = name; } } static void Main(string[] args) { var centers = new List<Center>(); centers.Add(new Center("Tennessee", "Knoxville")); centers.Add(new Center("Arizona", "Tuscon")); centers.Add(new Center("New Mexico", "Albuquerque")); centers.Add(new Center("Arizona", "Phoenix")); var query = centers.GroupBy(c => c.Place); foreach (var g in query) { Console.WriteLine(g.Key); foreach (var n in g) { Console.WriteLine("\t{0}", n.Name); } } } Output: Tennessee Knoxville Arizona Tuscon Phoenix New Mexico Albuquerque Regards.
From: shapper on 21 Jan 2010 22:27 On Jan 22, 2:20 am, kndg <re...(a)this.newsgroup> wrote: > On 1/22/2010 9:10 AM, shapper wrote: > > > Hello, > > > I have a list of objects: IList<Center> centers. Each Center has two > > properties: Place and Name. > > > I need to create a loops through the distinct Places in all centers > > and for each Place displays the centers names. > > > My problem is how here: > > > foreach (Place p in centers. ??? > > > Thanks, > > Miguel > > I take Mike data as example: > > public class Center > { > public string Place { get; private set; } > public string Name { get; private set; } > > public Center(string place, string name) > { > Place = place; > Name = name; > } > > } > > static void Main(string[] args) > { > var centers = new List<Center>(); > centers.Add(new Center("Tennessee", "Knoxville")); > centers.Add(new Center("Arizona", "Tuscon")); > centers.Add(new Center("New Mexico", "Albuquerque")); > centers.Add(new Center("Arizona", "Phoenix")); > > var query = centers.GroupBy(c => c.Place); > > foreach (var g in query) > { > Console.WriteLine(g.Key); > > foreach (var n in g) > { > Console.WriteLine("\t{0}", n.Name); > } > } > > } > > Output: > > Tennessee > Knoxville > Arizona > Tuscon > Phoenix > New Mexico > Albuquerque > > Regards. Thank You. That Group approach is really great. I am just using it. But thank you all. The other approach was good to. Thanks, Miguel
|
Next
|
Last
Pages: 1 2 Prev: FileInfo.CopyTo to a mapped or network drive Next: max size command line argument |