tags:

views:

36

answers:

1

I have an array of strings and need to convert them to a dataset. Is there some shortcut way to do this? For example:

string[] results = GetResults();    
DataSet myDataSet = new DataSet();

results = myDataSet.ToDataSet();  // Convert array to data set

I’m not too bothered about formatting or structure.

+2  A: 

I can't see any value in this but if you really must do as you request, the following code will create a dataset with a single table that has a single column and a row per item in the array.

internal static class Program
{
    private static void Main(string[] args)
    {
        string[] array = new [] { "aaa", "bbb", "ccc" };
        DataSet dataSet = array.ToDataSet();
    }

    private static DataSet ToDataSet(this string[] input)
    {
        DataSet dataSet = new DataSet();
        DataTable dataTable = dataSet.Tables.Add();
        dataTable.Columns.Add();
        Array.ForEach(input, c => dataTable.Rows.Add()[0] = c);
        return dataSet;
    }
}
Daniel Renshaw
very nice to do this with an extension method.
Wouter Janssens - Xelos bvba