MPA
MPA

Reputation: 1

How to convert a list<list<object>> to object[,]?

I have the following static class to build an dynamic object Array:

public class oDataArray
{
    public static List<List<object>> GetDataObject(DbCommand command)
    {
        List<List<object>> oDataList = new List<List<object>>();

        using (DbDataReader dr = command.ExecuteReader())
        {
            if (dr.HasRows)
            {
                while (dr.Read())
                {
                    List<object> oData = new List<object>();

                    for (int i = 0; i < dr.FieldCount; i++)
                    {
                        oData.Add(dr[i]);
                    }
                    oDataList.Add(oData);
                }
            }
            dr.Close();
        }
        return oDataList;
    }
}

I tried to convert it to a 2 dimensional object array via:

object[,] myNewObjectArray = oDataArray.GetDataObject(anyDbCommand).ToArray<object>().ToArray<object>();

But it will not work!? :( What I'm doing wrong?

Upvotes: 0

Views: 106

Answers (1)

Roland
Roland

Reputation: 71

How about,

oDataArray.GetDataObject(anyDbCommand).Select(a => a.ToArray()).ToArray();

Some explanation:

  • Select(a => a.ToArray()) crates an enumerable of type object[],
  • ToArray() creates an array of arrays.

Upvotes: 1

Related Questions