0

I know there are plenty of similar threads out there but my requirement is little different. The basic requirement is to convert a DataTable to array of strings. I am able to do that by creating an Extension method to do that work for me.

public static class ExtensionMethods
{
     public static object ToStringArray(this DataTable dt, params object[] columns)
     {
          //if specifically columns are provided then returns item array values for these columns.
          if (columns != null && columns.Length > 0)
          {
              //return dt.AsEnumerable().Select(x => new[] { //here i want to iterate through columns params }).ToArray();
          }
          else
          {
               return dt.AsEnumerable().Select(x => new[] { x.ItemArray }).ToArray();
          }
      }    
}

If I don't provide column names explicitly then it perfectly returns all rows values as strings array.

var dtData = dataTable.ToStringArray();

But what if i provide column names

var dtData = dataTable.ToStringArray("Column1", "Column2", "Column3");

then how can I iterate through columns to get only provided columns values in this chunk of code

if (columns != null && columns.Length > 0)
{
     return dt.AsEnumerable().Select(x => new[] { //here i want to iterate through columns params }).ToArray();
}

I know I can do that by hard coding the column names as follow:

var dtData = dt.AsEnumerable().Select(x => new[] { x["Column1"], x["Column2"], x["Column3"] }).ToArray();

or

var dtData = dt.AsEnumerable().Select(x => new[] { x.Field<string>("Column1"), x.Field<int>("Column2").ToString(), x.Field<string>("Column3") }).ToArray();

How can I get this working in my Extension method and by using param object[] columns values?

1 Answer 1

3

You could do a select on the columns parameter. I have changed the type from object[] to string[] here.

public static class ExtensionMethods
{
    public static object ToStringArray(this DataTable dt, params string[] columns)
    {
        //if specifically columns are provided then returns item array values for these columns.
        if (columns != null && columns.Length > 0)
        {
            return dt.AsEnumerable().Select(x => columns.Select(c => x[c]).ToArray()).ToArray();
        }
        else
        {
            return dt.AsEnumerable().Select(x => new[] { x.ItemArray }).ToArray();
        }
    }
}
Sign up to request clarification or add additional context in comments.

2 Comments

I would probably add ToArray to the columns.Select part: columns.Select(c => x[c]).ToArray(). Otherwise, a nice answer
You did it. Thanks you very much buddy. And @Pikoh thank you very much too, Your suggestion made this answer perfect according to my requirement.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.