Convert DataTable to Generic List in C#

后端 未结 9 1302
既然无缘
既然无缘 2020-12-01 01:28

Disclaimer: I know its asked at so many places at SO.
My query is a little different.

Coding Language: C# 3.5

I have a DataTable named cardsTable that

9条回答
  •  盖世英雄少女心
    2020-12-01 02:12

    The .ToList() is in the wrong place, and if some fields can be null you'll have to deal with these as they wont convert to Int64 if they're null

    DataTable dt = GetDataFromDB();
    List target = dt.AsEnumerable().Select(
      x => new Cards { CardID = (Int64)(x.ItemArray[0] ?? 0) }).ToList();
    

提交回复
热议问题