0

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

2 Answers 2

7

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);
}
Sign up to request clarification or add additional context in comments.

Comments

1

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}"))

1 Comment

That overload of string.Concat is available only in .NET 4.0

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.