LinqToDataTable

Posted on 2010-12-03 16:22  gmtyt  阅读(208)  评论(0)    收藏  举报
1 Public Function LinqToDataTable(ByVal list As IEnumerable) As DataTable
2 Dim dt As New DataTable()
3 Dim schemaIsBuild As Boolean = False
4 Dim props As PropertyInfo() = Nothing
5
6 For Each item As Object In list
7 If Not schemaIsBuild Then
8 props = item.[GetType]().GetProperties()
9 For Each pi In props
10 dt.Columns.Add(New DataColumn(pi.Name, pi.PropertyType))
11 Next
12
13 schemaIsBuild = True
14 End If
15
16 Dim row As DataRow = dt.NewRow()
17 For Each pi In props
18 row(pi.Name) = pi.GetValue(item, Nothing)
19 Next
20
21 dt.Rows.Add(row)
22 Next
23
24 dt.AcceptChanges()
25 Return dt
26 End Function

 

博客园  ©  2004-2025
浙公网安备 33010602011771号 浙ICP备2021040463号-3