I have an array or a list from linq. I want to show it as a string in console! What should I do?
+1
A:
Just iterate over it?
foreach (var item in list)
{
Console.WriteLine(item.ToString());
}
Davy Landman
2009-05-14 12:42:48
+7
A:
String.Join(delimiter, array);
You could represent it as:
Console.WriteLine("{" + String.Join(", ", array) + "}");
Of course, I think this only works with strings.
Brandon Montgomery
2009-05-14 12:42:59
+2
A:
The most generic answer that I can give to you is to loop through each element and use the ToString()
method on each element.
Alternatively, you can serialize the Array/List to Xml.
Trumpi
2009-05-14 12:43:01
Thanks, I decided to use Json serializer, its visually simple
dynback.com
2009-05-14 12:51:45
A:
Generally you can loop through it if it's a collection or an array. Check the foreach
keyword
List<Object> list = ...
foreach (Object o in list) {
Console.WriteLine(o.ToString);
}
André Neves
2009-05-14 12:43:44
A:
I would want some more information about exactly what you want to see, but at first blush I'd try something like:
public string StringFromArray(string[] myArray)
{
string arrayString = "";
foreach (string s in myArray)
{
arrayString += s + ", ";
}
return arrayString;
}
AllenG
2009-05-14 12:44:59
A:
If you'd like a more LINQ approach you could use the following:
String text = String.Join("," + Environment.NewLine, list.Select(item => item.ToString()).ToArray());
Console.WriteLine(text);
The first parameter of the Join specifies which characters should be inserted between items in the array. Using the .Select on the list is for getting a string representation of your item in the array.
Davy Landman
2009-05-14 12:48:43