Foreach Loop. Distinct

  • Thread starter Thread starter shapper
  • Start date Start date
S

shapper

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 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
 
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);
}
 
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.
 
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
 
Family said:
[...]
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);
}

Personally, were I to use something like the above (for this particular
question, it's inefficient) I would put the sort at the end of the
expression. Why sort more data than you have to?
 
shapper said:
That Group approach is really great. I am just using it. [...]

Grouping is the best approach given your problem statement. It
specifically arranges the data in exactly the way you need it in a
single pass, rather than requiring a nested level of iteration.

But, for future reference…if you do find yourself having some other need
for which the Enumerable.Distinct() method is best, note the overload
for that method that takes an IEqualityComparer<T> argument. That can
allow you to find elements that are distinct without losing the actual
element instance to a project via Select().

Frankly, the frequency such an approach is useful is probably pretty
low. But it's handy to know about, for when you do need it. :)

Pete
 
Let me try that again, this time without such awkward phrasing. Instead of:

Peter said:
[...] That can
allow you to find elements that are distinct without losing the actual
element instance to a project via Select().

I prefer:

"That can allow you to find elements that are distinct according to some
specific criteria without losing the actual element instance to a
projection via Select()."
 
Peter Duniho said:
Family said:
[...]
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);
}

Personally, were I to use something like the above (for this particular
question, it's inefficient) I would put the sort at the end of the
expression. Why sort more data than you have to?
.

Good point!

Mike
 
Back
Top