设为首页 加入收藏

TOP

Linq技术三:LinqtoObject和生成数据表的扩展方法(三)
2015-11-21 01:31:09 来源: 作者: 【 】 浏览:4
Tags:Linq 技术 LinqtoObject 生成 数据 扩展 方法
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多的循环,如同在操作数据库表一样。

??
首页 上一页 1 2 3 4 5 下一页 尾页 3/5/5
】【打印繁体】【投稿】【收藏】 【推荐】【举报】【评论】 【关闭】 【返回顶部
分享到: 
上一篇mybatis异常:元素内容必须由格式.. 下一篇对监听静态注册配置的理解

评论

帐  号: 密码: (新用户注册)
验 证 码:
表  情:
内  容: