我有以下功能 -如何将var转换为DataTable?
public static DataTable getDetails(PersonContext context)
{
DataTable dt = new DataTable();
IQueryable<Person> query = from p in context.Persons.Include("Employee")
.Include("Manager")
.Include("Activity")
where p.Activity.IsActive
select p;
var sorted = query.ToArray().OrderByDescending(p=>p.Activity.DateCreated);
dt = (DataTable)sorted;
return dt;
}
我无法测试它。 我的问题是 - 这个功能会起作用吗?如果我不应该做什么改变?
更新
public static DataTable getDetails(PersonContext context)
{
DataTable dt = new DataTable("Details");
dt.Columns.Add("Name");
dt.Columns.Add("Department");
dt.Columns.Add("IsManager");
IQueryable<Person> query = from p in context.Persons.Include("Employee")
.Include("Manager")
.Include("Activity")
where p.Activity.IsActive
select p;
var sorted = query.ToArray().OrderByDescending(p=>p.Activity.DateCreated);
foreach(Person p in sorted)
{
dt.Rows.Add(p.Name, p.Employee.Department,p.Manager.IsManager);
}
return dt;
}
找到更多的信息为什么你不能测试它? –
就这样,这将不起作用,因为'sorted'将会正确输入'IQueryable'。我相信你应该在消费方面更好地排序结果,而不是在数据表本身内。排序是表示层的功能,而不是数据层 –
检查这些解决方案[这里](http://www.dreamincode.net/forums/topic/85368-c%23-convert-object-array-into-datatable/)。 希望这会有所帮助。 –