From: Peter Duniho on 22 Jan 2010 00:25 Family Tree Mike wrote: > [...] > 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?
From: Peter Duniho on 22 Jan 2010 00:30 shapper wrote: > 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
From: Peter Duniho on 22 Jan 2010 00:37 Let me try that again, this time without such awkward phrasing. Instead of: Peter Duniho wrote: > [...] 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()."
From: Family Tree Mike on 22 Jan 2010 08:16 "Peter Duniho" wrote: > Family Tree Mike wrote: > > [...] > > 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
First
|
Prev
|
Pages: 1 2 Prev: FileInfo.CopyTo to a mapped or network drive Next: max size command line argument |