LINQ To objects: Quicker ideas?
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.Numbe开发者_StackOverflow社区r );
basenumbers.Aggregate
(
numbers,
( res, element ) => res.AppendFormat( "{0:00}", element )
);
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);
}
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}"))
精彩评论