w(values, options.Value)
Else
table.LoadDataRow(values, True)
End If
Loop
End Using
table.EndLoadData()
' Return the table.
Return table
End Function
Public Function ExtendTable(ByVal table As DataTable, ByVal type As Type) As DataTable
' Extend the table schema if the input table was null or if the value
' in the sequence is derived from type T.
Dim f As FieldInfo
Dim p As PropertyInfo
For Each f In type.GetFields
If Not Me._ordinalMap.ContainsKey(f.Name) Then
Dim dc As DataColumn
' Add the field as a column in the table if it doesn't exist
' already.
dc = IIf(table.Columns.Contains(f.Name), table.Columns.Item(f.Name), table.Columns.Add(f.Name, f.FieldType))
' Add the field to the ordinal map.
Me._ordinalMap.Add(f.Name, dc.Ordinal)
End If
Next
For Each p In type.GetProperties
If Not Me._ordinalMap.ContainsKey(p.Name) Then
' Add the property as a column in the table if it doesn't exist
' already.
Dim dc As DataColumn
dc = IIf(table.Columns.Contains(p.Name), table.Columns.Item(p.Name), table.Columns.Add(p.Name, p.PropertyType))
' Add the property to the ordinal map.
Me._ordinalMap.Add(p.Name, dc.Ordinal)
End If
Next
' Return the table.
Return table
End Function
End Class
'''
''' Provide public function for calling
'''
'''
Public Module CustomLINQtoDataSetMethods
_
Public Function CopyToDataTable(Of T)(ByVal source As IEnumerable(Of T)) As DataTable
Return New ObjectShredder(Of T)().Shred(source, Nothing, Nothing)
End Function
_
Public Function CopyToDataTable(Of T)(ByVal source As IEnumerable(Of T), ByVal table As DataTable, ByVal options As LoadOption?) As DataTable
Return New ObjectShredder(Of T)().Shred(source, table, options)
End Function
End Module
上面就是Linq to Object的一些操作方式,简便,不需要作N多的循环,如同在操作数据库表一样。
??