简体   繁体   中英

string.Join throws OutOfMemory Exception

I have a list of strings and I want to join them with " "(space) between them, so I use the string.Join method:

foreach (var line in lines)
{        
    var strings = lines.Where(l => l.Code == line.Code).Select(l => l.Data);
    var original = string.Join(" ", strings);        
}

Data looks something like this: "123456789, 987654321, 32132, 7873892 ..."

But I get an OutOfMemoryException. Why? each string is approximatly 100-150 characters and there are 5-10 strings in the list.

Is there a better way then the string.Join?

试试这个(让我们知道您是否遇到相同的错误):

lines.GroupBy(l => l.Code).Select(l => string.Join(" ", l.Select (x => x.Data)));
foreach (var line in lines.GroupBy(p=>p.Code))
{        
    var original = string.Join(" ", line.Select(p=>p.Data));        
}

The StringBuild() class can join strings and isn't immutable.

Here's an MSDN article talking about immutable string vs how StringBuilder works. http://msdn.microsoft.com/en-us/library/2839d5h5(v=vs.71).aspx

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM