Convert Array/List into Comma Separated String Using LINQ In C#.Net

How to convert/change array or list of string or int into a comma separated string using LIN in c#.net. Here use string.Join to get string value.
In today's article I will show you how you can convert an array of string type and a List of string type into a comma separated string using LINQ using c#.net. For this we will create a new console application in in your windows or .NET core application. In your console application first, we will take a list or array which is having some values either it is number or string value. Here we will cover both first we will take Array.

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. 

code to convert array to string using c#.net

Here in above code, you can see the array have been converted into the string value. Now lets press F5 to check the output.

Convert Array Into Comma Separated String Using LINQ In C#.Net

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.

Convert List Into Comma Separated String Using LINQ In C#.Net

In above code provided list have been converted into a comma separated string. Now let's press F5 to check final output.

Convert List Into Comma Separated String Using LINQ In C#.Net

Post a Comment