【发布时间】:2010-11-05 08:47:13
【问题描述】:
免责声明:我知道它在 SO 的很多地方都被问到过。
我的查询有点不同。
编码语言:C# 3.5
我有一个名为cardsTable 的数据表,它从数据库中提取数据,我有一个类 Cards,它只有一些属性(没有构造函数)
public class Cards
{
public Int64 CardID { get; set; }
public string CardName { get; set; }
public Int64 ProjectID { get; set; }
public Double CardWidth { get; set; }
public Double CardHeight { get; set; }
public string Orientation { get; set; }
public string BackgroundImage { get; set; }
public string Background { get; set; }
}
我想将 cardsTable 数据插入到 List 类型的对象中。
我的数据中将包含空字段,因此当我转换数据时该方法不应出错。以下方法是最好的方法吗?
DataTable dt = GetDataFromDB();
List<Cards> target = dt.AsEnumerable().ToList().ConvertAll(x => new Cards { CardID = (Int64)x.ItemArray[0] });
【问题讨论】:
-
这个问题是要关闭还是一个错误? :)
-
@marc garvell:我也想操作数据表数据...
-
你确实有一个构造函数......默认的。
标签: c# linq generics lambda datatable