开发者

Get an array of IDs(values) from a datatable

I have a datable with 50 rows and has an ID Column. I am trying to get an array that holds only the IDs like:

string [] IDs = (from row in DataTable.Rows
                select row["ID"].toString()).ToArray();

Is there a way to do this. I always get the error "Could开发者_JAVA百科 not find he implementation of the query...."


Use the DataTableExtensions.AsEnumerable method by adding a reference to System.Data.DataSetExtensions and a using System.Data; Then you should be able to use the following query:

var query = from row in datatable.AsEnumerable()
            select row["ID"].ToString();
string[] ids = query.ToArray();

If you really need an array you can use the last line above or enclose the query in parentheses and call ToArray() as you did originally. I'm generally not a fan of the latter approach.

In fluent syntax it would be:

string[] ids = datatable.AsEnumerable()
                        .Select(row => row["ID"].ToString())
                        .ToArray();


is there is anyway you can select a list data table into a customer object array. Assuming all the columns are going to be same.

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜