2

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?

gdoron
  • 147,333
  • 58
  • 291
  • 367

3 Answers3

4

Try this (and let us know if you get the same error):

lines.GroupBy(l => l.Code).Select(l => string.Join(" ", l.Select (x => x.Data)));
Magnus
  • 45,362
  • 8
  • 80
  • 118
2
foreach (var line in lines.GroupBy(p=>p.Code))
{        
    var original = string.Join(" ", line.Select(p=>p.Data));        
}
Magnus
  • 45,362
  • 8
  • 80
  • 118
Reza ArabQaeni
  • 4,848
  • 27
  • 46
-1

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

user1231231412
  • 1,659
  • 2
  • 26
  • 42
  • 1
    I'm not the downvoter, but I'm not curious as to what this has to do with gdoron's question. – Steve Rukuts Dec 06 '11 at 21:30
  • @Raskolnikov He asked "Is there a better way then the string.Join?" My answer was SringBuilder. The OutOfMemory could be caused by a million unrelated things. My understanding is that StringBuilder is the suggested method by MS to concatenate a bunch of strings. I don't think my suggestion was completely off-topic?! – user1231231412 Dec 06 '11 at 21:39
  • @JonC: (I'm not the downvoter either) `String.Join()` is actually more efficient than `StringBuilder` because it can determine the lengths before-hand and build the exact sized buffer. – James Michael Hare Dec 06 '11 at 21:42
  • @JonC `String.Join` can actually be faster than `StringBuilder` see:http://stackoverflow.com/questions/585860/string-join-vs-stringbuilder-which-is-faster – Magnus Dec 06 '11 at 21:43
  • @JamesMichaelHare I've read that as well but it always says "in some scenarios" I'm not sure if that fits the OPs scenario but saying StringBuilder as an alternative for string.join I would not think deserves a down vote? – user1231231412 Dec 06 '11 at 21:46
  • @JonC: I understand and accept your explanation. Also I noticed that I said "I'm not curious as to...". No idea what happened there, but I guess my question was obvious with the context. – Steve Rukuts Dec 06 '11 at 21:58
  • @JonC: I'm not the down-voter, so I don't know why he was down-voted. Sometimes that happens unfortunately and you never know who or why. Sometimes, unfortunately, it's even a petty attempt by some answers to push their answer up the list, but that's *really* rare. – James Michael Hare Dec 06 '11 at 21:58
  • @JamesMichaelHare No worries. I'm here to payback for the help I get from SO, not so much for "points". I just don't want to be a leech really. It would just help if someone told me what I was doing wrong with a downote. – user1231231412 Dec 06 '11 at 22:40
  • @JonC: I know, I really wish there was a place for a downvote comment (even if anonymous). I know they can put it in the comments, of course, but often they don't. – James Michael Hare Dec 07 '11 at 00:47