Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/csharp/280.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
C# 如何将var转换为DataTable?_C#_Asp.net_Linq_Entity Framework - Fatal编程技术网

C# 如何将var转换为DataTable?

C# 如何将var转换为DataTable?,c#,asp.net,linq,entity-framework,C#,Asp.net,Linq,Entity Framework,我有以下功能- public static DataTable getDetails(PersonContext context) { DataTable dt = new DataTable(); IQueryable<Person> query = from p in context.Persons.Include("Employee") .Include("Manager")

我有以下功能-

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;
}
公共静态数据表getDetails(PersonContext上下文)
{
DataTable dt=新的DataTable();
IQueryable query=来自context.Persons.Include(“员工”)中的p
.包括(“经理”)
.包括(“活动”)
其中p.Activity.IsActive
选择p;
var sorted=query.ToArray().OrderByDescending(p=>p.Activity.DateCreated);
dt=(数据表)已排序;
返回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;
}
公共静态数据表getDetails(PersonContext上下文)
{
DataTable dt=新的DataTable(“详细信息”);
dt.列。添加(“名称”);
dt.栏添加(“部门”);
dt.列。添加(“IsManager”);
IQueryable query=来自context.Persons.Include(“员工”)中的p
.包括(“经理”)
.包括(“活动”)
其中p.Activity.IsActive
选择p;
var sorted=query.ToArray().OrderByDescending(p=>p.Activity.DateCreated);
foreach(排序中的人员p)
{
dt.Rows.Add(p.Name、p.Employee.Department、p.Manager.IsManager);
}
返回dt;
}

如果
OrderByDescending
的返回类型为
数据表
,则不需要强制转换。编译器在编译时知道方法返回类型的类型


但是,
OrderByDescending
不返回需要构造的
DataTable

如果
OrderByDescending
的返回类型为
DataTable
,则不需要强制转换。编译器在编译时知道方法返回类型的类型

但是,
OrderByDescending
不返回需要构造的
DataTable。

OrderByDescending()返回的是IEnumerable而不是DataTable

这个链接回答了似乎相同的问题。

OrderByDescending()返回IEnumerable而不是DataTable

这个链接回答了似乎相同的问题。

是无效的类型转换。正确的方法是使用
将返回的数组添加到dt
dt.Rows.Add(query.ToArray().OrderByDescending(p=>p.Activity.DateCreated.ToArray())
您可以在

是无效的类型转换。正确的方法是使用
将返回的数组添加到dt
dt.Rows.Add(query.ToArray().OrderByDescending(p=>p.Activity.DateCreated.ToArray())
您可以在

上找到更多信息,谢谢@prthrokz。请查看更新。现在可以了吗?首先从Person创建一个DataRow。DataRow dr=dt.NewRow();然后将必要的值添加到行中,dr[“Name”]=p.Name;dr[“Dept”]=p.Employee.Department等等。最后是dt.Rows.Add(dr);所有这些都发生在foreach循环中。谢谢@prthrokz。我跟着你走。不过,我不能测试它,但我假设它现在可以工作了。谢谢@prthrokz。请查看更新。现在可以了吗?首先从Person创建一个DataRow。DataRow dr=dt.NewRow();然后将必要的值添加到行中,dr[“Name”]=p.Name;dr[“Dept”]=p.Employee.Department等等。最后是dt.Rows.Add(dr);所有这些都发生在foreach循环中。谢谢@prthrokz。我跟着你走。然而,我不能测试它,但我假设它现在可以工作了。检查那些解决方案。希望这会有所帮助。照目前的情况,这不会起作用,因为
已排序
将正常键入
IQueryable
。我认为您应该更好地在消费端对结果进行排序,而不是在数据表本身中。排序是表示层的功能,而不是数据层检查这些解决方案。希望这会有所帮助。照目前的情况,这不会起作用,因为
已排序
将正常键入
IQueryable
。我认为您应该更好地在消费端对结果进行排序,而不是在数据表本身中。排序是表示层的功能,而不是数据层
dt = (DataTable)sorted;