0

I'm trying to convert a DataTable's rows into multidimensional array:

DataTable DT = new DataTable();
DT.Columns.AddRange
(
   new DataColumn[]
   { 
           new DataColumn("LA_ID"), 
           new DataColumn("contractid") 
   }
);

for(int i=1000; i<=1100; i++)
   DT.Rows.Add(i, i);

EnumerableRowCollection<DataRow> appsEnum = DT.AsEnumerable();

int[,] apps = appsEnum.
               Where(x => x.Field<int>("LA_ID") > 1050).
               Select(x => x.Field<int>("LA_ID"), x.Field<int>("contractid")).
--Error Here-------------------------------------^
               ToArray();

Can anyone help plz.

enter image description here

2
  • compile time it's because of syntax issue I believe. Commented Mar 14, 2015 at 8:01
  • Does it throw an exception? Post the exception. Does the LA_ID property exist? Commented Mar 14, 2015 at 8:02

2 Answers 2

2

There is no version of .ToArray() that supports more than 1 dimension. You will need a for-loop here.

It could probably work with a int[][] but not for int[,]

Sign up to request clarification or add additional context in comments.

1 Comment

It absolutely works with a int[][] but not for int[,]
1

the best you can get with ToArray() method is jagged array

int [][] apps = appsEnum.
           Where(x => x.Field<int>("laid") > 1050).
           Select(x => new int[] {x.Field<int>("LA_ID"), x.Field<int>("contractid")}).
           ToArray();

consider another approach

DataRow[] rows = DT.Select("laid > 1050");

int[,] apps = new int[rows.Length, 2];

for(int r=0; r<rows.Length; r++)
{
   apps[r,0] = rows[r].Field<int>("LA_ID");
   apps[r,1] = rows[r].Field<int>("contractid");
}

Comments

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.