1

Possible Duplicate:
Concat all strings inside a List<string> using LINQ

I am using C# 4.0, where I have a list non-null of string objects.

IList<String> Errors

What I want to do is to create a single string which has all list elements appended, one after another, with a new line character.

public String  ErrorMessage
{
    get { return Errors.SomeMethodHere(); }
}

One way I could think of is to loop on list of string. Is there any better way or in built System.String or LINQ method which I can use for this?

Community
  • 1
  • 1
Maheep
  • 5,539
  • 3
  • 28
  • 47
  • I think the best way is probably to loop through like you said – annonymously Dec 02 '11 at 07:59
  • 3
    http://stackoverflow.com/questions/559415/concat-all-strings-inside-a-liststring-using-linq – Piotr Auguscik Dec 02 '11 at 08:00
  • What's wrong with this question? Why 3 down votes? – Maheep Dec 02 '11 at 08:10
  • There is nothing wrong with the question. Probably, you wouldn't have asked it here since the solution is very simple. – Zaldy Baguinon Dec 02 '11 at 08:12
  • 1
    This can be simple for someone, but I have never used this hence asked about that. Does simple solution mean down votes? – Maheep Dec 02 '11 at 08:19
  • 1
    by that logic http://stackoverflow.com/users/22656/jon-skeet should down vote every question he answers. – Maheep Dec 02 '11 at 08:20
  • +1 to get the votes back to 0 :) – Holystream Dec 02 '11 at 08:24
  • @Zaldy Baguinon There are no simple questions. The very fact that it is a question means that it needs an answer and hence has been asked. A question that you find difficult might be simple to others. raison d'etre for stackoverflow is not classifying questions easy or hard but to answer questions. – P.K Dec 02 '11 at 11:48
  • @P.K.: I thought that is the reason they down voted. I didn't down vote Maheeps question. I'm still new here perhaps some members think that way. – Zaldy Baguinon Dec 05 '11 at 08:26

5 Answers5

5
String.Join(Environment.NewLine, Errors.ToArray());
fixagon
  • 5,506
  • 22
  • 26
  • In .NET 4 you don't need the `ToArray`: [`String.Join`](http://msdn.microsoft.com/en-us/library/dd783876.aspx) has overloads that take `IEnumerable` for the second parameter. – Richard Dec 02 '11 at 08:08
  • Not necessary to use `.ToArray()`, .Join method also allow IList type data. – Elias Hossain Dec 02 '11 at 08:10
  • there are still some environments which use 3.5 (even 2.0 imagine!) --> no overload for IList – fixagon Dec 02 '11 at 08:14
2

Try String.Join(Environment.NewLine, Errors.ToArray()) (for .NET 4 and up you don't need the ToArray)

More Info: http://msdn.microsoft.com/en-us/library/57a79xd0.aspx

Botz3000
  • 39,020
  • 8
  • 103
  • 127
1
public String ErrorMessage
{
    get 
    { 
        //Use your appropriate separator instead of ','
        return string.Join(",", Errors); 
        //return string.Join("", Errors); // Just concatenating all message 
    }
}
Elias Hossain
  • 4,410
  • 1
  • 19
  • 33
0
Errors.Aggregate((left, right) => string.Format(@"{0}\n{1}", left, right));
Harps
  • 640
  • 8
  • 17
0
 public static class MyExtensions
{
    public static string ErrorMessage(this IList<String> strList)
    {
        string retMessage = null;
        for (int i = 0; i < strList.Count; i++)
        {
            retMessage += strList[i].ToString() + Environment.NewLine;
        }
        return retMessage;
    }

}

you can use the above code snippest to make an extended method to generate single string from list data.

Hiren Visavadiya
  • 485
  • 1
  • 3
  • 15