views:

97

answers:

2

Do you see a better approach to obtain and concatenate item.Number in a single string?

Current:

var numbers = new StringBuilder( );
// group is the result of a previous group by
var basenumbers = group.Select( item => item.Number );
basenumbers.Aggregate
(
  numbers,
  ( res, element ) => res.AppendFormat( "{0:00}", element )
);
+7  A: 

A foreach will be slightly simpler and easier to understand.

var numbers = new StringBuilder();

foreach(var number in group.Select(item => item.Number))
{
    numbers.AppendFormat("{0:00}", number);
}
Adam Robinson
+1  A: 

Maybe you don't really need to use StringBuilder explicitly here - the String.Concat method should be (even more) efficient for concatenation. However, I'm not sure whether calling ToString for all elements like this is a performance issue if you use it like this (I wouldn't think so - the main issue with +ing strings is copying):

String.Concat(grp.Select(item => item.Number.ToString("{0:00}"))
Tomas Petricek
That overload of `string.Concat` is available only in .NET 4.0
Adam Robinson