开发者

LINQ To objects: Quicker ideas?

开发者 https://www.devze.com 2023-01-01 05:22 出处:网络
Do you see a better approach to obtain and concatenate item.Number in a single string? Current: var numbers = new StringBuilder( );

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

精彩评论

暂无评论...
验证码 换一张
取 消