Convert Array in Comma Separated String by LINQ Using C#.Net?
Here we will convert an array into comma separated string value using LINQ in c#.net. This you can use in your .NET core as well as in your windows or console application. First, we will take an array.
int[] integerArray= [1,2,3,4,5,6,7,8,9,10];
Now let's check the LINQ query to convert the array into and comma separated string. int[] integerArray= [1,2,3,4,5,6,7,8,9,10];
//Code to convert the array into string.
string finalString = string.Join(",", integerArray);
Console.WriteLine("Array List: [1,2,3,4,5,6,7,8,9,10]");
Console.WriteLine("Comma Separated String: ");
Console.WriteLine(finalString);
In above code i have taken an array named as integerArray and check the highlighted code. String.Join() method is used for converting the array into the comma separated string. Now lets run the code and check the output by putting a break point. Convert List in Comma Separated String by LINQ Using C#.Net?
Here we are going to show how we can convert the list into an comma separated string using c#.net. Here we are going to use console application to show the example. Please check the below list code.
List<int> intList = new List<int>();
intList.Add(0);
intList.Add(1);
intList.Add(2);
intList.Add(3);
intList.Add(4);
intList.Add(5);
intList.Add(6);
intList.Add(7);
intList.Add(8);
In above list named as intList I have added 8 numbers. Now lets check the complete code to convert the list into string using c#.net.List<int> intList = new List<int>();
intList.Add(0);
intList.Add(1);
intList.Add(2);
intList.Add(3);
intList.Add(4);
intList.Add(5);
intList.Add(6);
intList.Add(7);
intList.Add(8);
//Code to convert the array into string.
string finalString = string.Join(",", intList);
Console.WriteLine("Comma Separated String: ");
Console.WriteLine(finalString);
In above code please check the highlighted part of the code. In this i have used string.Join() method same as I used in Aary to convert the list into string. Now lets put a break point and check the output.