将匿名类型转换为 DataTable

发布于 2024-12-02 18:14:27 字数 111 浏览 1 评论 0原文

将匿名类型转换为 DataTable 的最快方法是什么?

更新: 我想从匿名类型获取并填充 DataTable。如果需要反射,我该如何使用反射来做到这一点?

What is the fastest way to convert anonymous type to DataTable?

Update:
I want to get and populate DataTable from anonymous type. If reflection is neccesary, how can I to do it using reflection?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(3

轮廓§ 2024-12-09 18:14:27

找到此处

var result = from p in dataSource 
             group p by p.City into cities 
             select new { Property1 = cities.Key, Property2 = cities.Average(p => p.Age) }; 

dt.Columns.Add("Property1"); 
dt.Columns.Add("Property2"); 
foreach (var item in result) 
{   
    dt.Rows.Add(item.Property1,item.Property2);                 
}

请参阅此处了解通用解决方案:将通用列表/枚举转换为数据表?

Found here:

var result = from p in dataSource 
             group p by p.City into cities 
             select new { Property1 = cities.Key, Property2 = cities.Average(p => p.Age) }; 

dt.Columns.Add("Property1"); 
dt.Columns.Add("Property2"); 
foreach (var item in result) 
{   
    dt.Rows.Add(item.Property1,item.Property2);                 
}

See here for a generic solution: Convert generic List/Enumerable to DataTable?

尘曦 2024-12-09 18:14:27
///fill dt1
Dim dt1 As New DataTable  dt1 = connection.LoadPoliceData("")
///fll dt2
Dim dt2 As New DataTable  dt2 = connection.LoadDataCompare("")
////fill enumerable data(anonymous data) in dt , using linq query
Dim dt As New DataTable
dt.Columns.Add("Name", GetType(String))
dt.Columns.Add("Mobile", GetType(String))
Dim data1 = (From datarow1 In dt1.AsEnumerable Join datarow2 In  dt2.AsEnumerable
              On datarow1.Field(Of String)("NameofPerson") Equals  datarow2.Field(Of String)("Name") And datarow1.Field(Of String)("Mobile") Equals datarow2.Field(Of String)("MobileNumber")
              Select dt.LoadDataRow(New Object() {datarow1.Field(Of String)("NameofPerson"), datarow2.Field(Of String)("MobileNumber")}, False)).Distinct().ToList()

Dim i = dt.Rows.Count

dt 中的记录(数据表变量)

///fill dt1
Dim dt1 As New DataTable  dt1 = connection.LoadPoliceData("")
///fll dt2
Dim dt2 As New DataTable  dt2 = connection.LoadDataCompare("")
////fill enumerable data(anonymous data) in dt , using linq query
Dim dt As New DataTable
dt.Columns.Add("Name", GetType(String))
dt.Columns.Add("Mobile", GetType(String))
Dim data1 = (From datarow1 In dt1.AsEnumerable Join datarow2 In  dt2.AsEnumerable
              On datarow1.Field(Of String)("NameofPerson") Equals  datarow2.Field(Of String)("Name") And datarow1.Field(Of String)("Mobile") Equals datarow2.Field(Of String)("MobileNumber")
              Select dt.LoadDataRow(New Object() {datarow1.Field(Of String)("NameofPerson"), datarow2.Field(Of String)("MobileNumber")}, False)).Distinct().ToList()

Dim i = dt.Rows.Count

record in dt(datatable variable )

川水往事 2024-12-09 18:14:27

///汇总填充数据表一 (dt1)

Dim dt1 As New DataTable

dt1 = connection.LoadPoliceData("")

///汇总填充数据表一 (dt2)

Dim dt2 As New DataTable

dt2 = connection.LoadDataCompare("")

// /summary 声明数据表保存。我想要填充可枚举数据的位置

Dim save As New DataTable

save.Columns.Add("Name", GetType(String))

save.Columns.Add("Mobile", GetType(String))

///总结使用 join 编写 linq 查询dt1 和 dt2 ,以及 datatable(save)

Dim data1 = (来自 datarow1 中的 dt1.AsEnumerable
在 dt2.AsEnumerable 中加入 datarow2
在 datarow1.Field(Of String)("NameofPerson") 等于 datarow2.Field(Of String)("Name") 和 datarow1.Field(Of String)("Mobile") 等于 datarow2.Field(Of String)("MobileNumber ”)
选择 save.LoadDataRow(New Object() {datarow1.Field(Of String)("NameofPerson"), datarow2.Field(Of String)("MobileNumber")}, False)).Distinct().ToList()

// /数据表保存的摘要计数

Dim i = save.Rows.Count

///summary fill datatable one (dt1)

Dim dt1 As New DataTable

dt1 = connection.LoadPoliceData("")

///summary fill datatable one (dt2)

Dim dt2 As New DataTable

dt2 = connection.LoadDataCompare("")

/// summary declare datatable save . where i want fill Enumerable data

Dim save As New DataTable

save.Columns.Add("Name", GetType(String))

save.Columns.Add("Mobile", GetType(String))

///summary writing linq query with join of dt1 and dt2 , And datatable(save)

Dim data1 = (From datarow1 In dt1.AsEnumerable
Join datarow2 In dt2.AsEnumerable
On datarow1.Field(Of String)("NameofPerson") Equals datarow2.Field(Of String)("Name") And datarow1.Field(Of String)("Mobile") Equals datarow2.Field(Of String)("MobileNumber")
Select save.LoadDataRow(New Object() {datarow1.Field(Of String)("NameofPerson"), datarow2.Field(Of String)("MobileNumber")}, False)).Distinct().ToList()

///summary count of datatable save

Dim i = save.Rows.Count

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文