Foreach Loop. Distinct

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
 
A

Arne Vajhøj

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
 
F

Family Tree Mike

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);
}
 
K

kndg

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.
 
S

shapper

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
 
P

Peter Duniho

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?
 
P

Peter Duniho

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
 
P

Peter Duniho

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()."
 
F

Family Tree Mike

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
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Similar Threads

Find Ints in List ... 4
Enumeration 2
Generics 7
Parsing Data Problem ... 5
GroupBy 6
Loop an Enumeration 4
Intel NUC Poor WiFi - External Antenna Mod 7
Linq Query and Lambda 3

Top