C# code to read distinct value from each "Column" from DataTable and storing them in comma separated value
public string GetColumnValueInString(DataTable dt, string ColumnName)
{
string result = string.Empty;
try
{
if (!string.IsNullOrEmpty(ColumnName))
{
DataTable uniqueCols = dt.DefaultView.ToTable(true, ColumnName);
result = string.Join(",", uniqueCols.Rows.Cast().Select(row => row[0].ToString()).ToArray());
}
}
catch (Exception ex)
{
Logging.Exception("GetColumnInString(DataTable dt, string ColumnName)", ex);
}
return result;
}
So The above function will take Data Table and Column name as input and stores the columns value in string.
So lets say i have table with column name "Age" then i can say:
string strAge=GetColumnInString(dt, "Age"));
This will give me all the value in the Age column with "," separated.
~ Happy Coding...
public string GetColumnValueInString(DataTable dt, string ColumnName)
{
string result = string.Empty;
try
{
if (!string.IsNullOrEmpty(ColumnName))
{
DataTable uniqueCols = dt.DefaultView.ToTable(true, ColumnName);
result = string.Join(",", uniqueCols.Rows.Cast
}
}
catch (Exception ex)
{
Logging.Exception("GetColumnInString(DataTable dt, string ColumnName)", ex);
}
return result;
}
So The above function will take Data Table and Column name as input and stores the columns value in string.
So lets say i have table with column name "Age" then i can say:
string strAge=GetColumnInString(dt, "Age"));
This will give me all the value in the Age column with "," separated.
~ Happy Coding...
No comments:
Post a Comment