由于查询返回 DataRow 类型的 IEnumerable,因此您必须指定要插入数据表的内容,在本例中为 DataRow。
DataTable dt = query.CopyToDataTable<DataRow>();
如果你用过
var query = //linq query of what you need for new datatable....
DataTable dt = query.CopyToDataTable();
你的表名是 dt 所以从原始 dt 中选择你需要的
var query = from c in db.something
where c.othersomething == "onlyyouknow"
orderby c.othersomething
select new { NewObject = c.othersomething };
DataTable MyDataTable = new DataTable();
myDataTable.Columns.Add(
new DataColumn()
{
DataType = System.Type.GetType("System.String"),//or other type
ColumnName = "Name" //or other column name
}
);
foreach (var element in query)
{
var row = MyDataTable.NewRow();
row["Name"] = element.NewObject;
myDataTable.Rows.Add(row);
}