My datatable consists of a column named "ID". The no. of values in this column varies.Sometimes this Datatable fetches 3 IDs in that ID column, sometimes 2. Now if for example, my datatable has three values as 1,2,3. What I want is to put these three values in a string and separate them by commas as folows:-
string test= "1,2,3";
If Datatable has 2 values, then string should be as follows:-
string test= "1,2";
I did try but in vain. Please help. Thanks.
edit:-
DataTable dt=new DataTable;
dt = obj.GetIDs();
for (int i = 0; i < dt.Rows.Count; i++)
{
string test= "What should be here????";
}
edit 2
foreach(DataRow dr in dt.Rows)
{
string str = str + "," + Convert.ToString(dr("ID"));
}
@Rajeev ::Tried this..it says dr is a variable but used as a method. What's wrong?
Select(s => s. Field<string>("Name")). ToArray(); string commaSeperatedValues = string. Join(",", SelectedValues);
In the ADO.NET library, C# DataTable is a central object. It represents the database tables that provide a collection of rows and columns in grid form. There are different ways to create rows and columns in the DataTable.
Just a one liner using LINQ.
String result = table.AsEnumerable()
.Select(row => row["ID"].ToString())
.Aggregate((s1, s2) => String.Concat(s1, "," + s2));
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