Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Getting a cell from DataTable.Row.ItemArray with Linq

I have the following ItemArray:

dt.Rows[0].ItemArray.. //{0,1,2,3,4,5}

the headers are : item0,item1,item2 etc..

So far, to get a value from the ItemArray I used to call it by an index.

Is there any way to get the value within the ItemArray with a Linq expression based on the column name?

Thanks

like image 268
David Rasuli Avatar asked Dec 05 '25 22:12

David Rasuli


1 Answers

You can also use the column-name to get the field value:

int item1 = row.Field<int>("Item1");
  • DataRow.Item Property(String)
  • DataRow.Field Method: Provides strongly-typed access

You could also use LINQ-to-DataSet:

int[] allItems = (from row in dt.AsEnumerable()
                  select row.Field<int>("Item1")).ToArray();

or in method syntax:

int[] allItems = dt.AsEnumerable().Select(r => r.Field<int>("Item1")).ToArray();
like image 137
Tim Schmelter Avatar answered Dec 07 '25 12:12

Tim Schmelter



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!