0

I am trying to convert a DataTable to array using Select and toArray I did this:

List<int> LevelsArray = CardServiceDetailsDt.Select("LEVEL_ID").ToList<int>(); 

but an error is accrued

'System.Data.DataRow[]' does not contain a definition for 'ToArray' and the best extension method overload 'System.Linq.Enumerable.ToArray<TSource>(System.Collections.Generic.IEnumerable<TSource>)' has some invalid arguments

3 Answers 3

1

Select will filter the rows for you but looks like you want to get all the values in one column, so you can't use Select, try this instead:

var rows = CardServiceDetailsDt.AsEnumerable()
                 .Select(row=>row.Field<long>("LEVEL_ID")).ToArray();
Sign up to request clarification or add additional context in comments.

4 Comments

it gives me a the specific cast is not valid
@Sora what's the actual type of LEVEL_ID?
it's ok i figure out that the actual type of LEVEL_ID is long
Make sure you have imported: using System.Data;
0

You can use AsEnumerable() to call ToArray() also use Field to ge the column value of datatable.

ObjDt.AsEnumerable().Select
            (r => r.Field<string>("LEVEL_ID")).ToArray();

Comments

0
var array = CardServiceDetailsDt.Rows[i]
                                .ItemArray
                                .Select(x => x.ToString())
                                .ToArray();

i is the index of the row.

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.