I have a DataTable that returns
IDs ,1 ,2 ,3 ,4 ,5 ,100 ,101
I want to convert this to single string value, i.e:
,1,2,3,4,5,100,101
How can i rewrite the following to get a single string
var _values = _tbl.AsEnumerable().Select(x => x);
The standard solution to convert a List<string> to a comma-separated string in C# is using the string. Join() method. It concatenates members of the specified collection using the specified delimiter between each item.
In Java, we can use String. join(",", list) to join a List String with commas.
Use the String. split() method to convert a comma separated string to an array, e.g. const arr = str. split(',') . The split() method will split the string on each occurrence of a comma and will return an array containing the results.
Use the join() Function to Convert a List to a Comma-Separated String in Python. The join() function combines the elements of an iterable and returns a string. We need to specify the character that will be used as the separator for the elements in the string.
var singleString = string.Join(",", _values.ToArray() );
Write an extension method such as
public static String AppendAll(this IEnumerable<String> collection, String seperator) { using (var enumerator = collection.GetEnumerator()) { if (!enumerator.MoveNext()) { return String.Empty; } var builder = new StringBuilder().Append(enumerator.Current); while (enumerator.MoveNext()) { builder.Append(seperator).Append(enumerator.Current); } return builder.ToString(); } }
and assuming the result of your previous expression is IEnumerable<String>, call:
var _values = _tbl.AsEnumerable().Select(x => x).AppendAll(String.Empty);
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With