博客
关于我
强烈建议你试试无所不能的chatGPT,快点击我
Ilist To DataTable
阅读量:4042 次
发布时间:2019-05-24

本文共 6115 字,大约阅读时间需要 20 分钟。

我们在项目中经常会使用到 List 转为 DataTable

方法一. 通过 扩展方法的方式,写一个IList接口的扩展方法

using System;using System.Collections.Generic;using System.Data;using System.ComponentModel;namespace DocumentSearch{
public static class DataTableExtensions {
public static DataTable ToDataTable
(this IList
data) {
PropertyDescriptorCollection properties = TypeDescriptor.GetProperties(typeof(T)); DataTable table = new DataTable(); foreach (PropertyDescriptor prop in properties) table.Columns.Add(prop.Name, Nullable.GetUnderlyingType(prop.PropertyType) ?? prop.PropertyType); foreach (T item in data) {
DataRow row = table.NewRow(); foreach (PropertyDescriptor prop in properties) row[prop.Name] = prop.GetValue(item) ?? DBNull.Value; table.Rows.Add(row); } return table; } }}调用时:var users = dbContext.Users.ToList().ToDataTable();或var users = dbContext.Users.ToListAsync().Result.ToDataTable();注意:用异步转为list,则要等 有result后再转为 datatable.

方法二.写一个自定义的类

using System;using System.Collections;using System.Collections.Generic;using System.Linq;using System.Text;using System.Data;using System.Reflection;using System.ComponentModel;namespace DotNet.Utilities{
/// /// IList 公共帮助类 /// public class IListHelper {
/// /// IList如何转成List
///
///
/// ///
public static List
IListToList
(IList list) {
T[] array = new T[list.Count]; list.CopyTo(array, 0); return new List
(array); } ///
/// Convert a List{T} to a DataTable. /// public static DataTable ToDataTable
(List
items) {
var tb = new DataTable(typeof(T).Name); PropertyInfo[] props = typeof(T).GetProperties(BindingFlags.Public | BindingFlags.Instance); foreach (PropertyInfo prop in props) {
Type t = GetCoreType(prop.PropertyType); tb.Columns.Add(prop.Name, t); } foreach (T item in items) {
var values = new object[props.Length]; for (int i = 0; i < props.Length; i++) {
values[i] = props[i].GetValue(item, null); } tb.Rows.Add(values); } return tb; } ///
/// Determine of specified type is nullable /// public static bool IsNullable(Type t) {
return !t.IsValueType || (t.IsGenericType && t.GetGenericTypeDefinition() == typeof(Nullable<>)); } ///
/// Return underlying type if type is Nullable otherwise return the type /// public static Type GetCoreType(Type t) {
if (t != null && IsNullable(t)) {
if (!t.IsValueType) {
return t; } else {
return Nullable.GetUnderlyingType(t); } } else {
return t; } } public static DataTable ToDataTable
(IList
data) { PropertyDescriptorCollection properties = TypeDescriptor.GetProperties(typeof(T)); DataTable table = new DataTable(); foreach (PropertyDescriptor prop in properties) table.Columns.Add(prop.Name, Nullable.GetUnderlyingType(prop.PropertyType) ?? prop.PropertyType); foreach (T item in data) { DataRow row = table.NewRow(); foreach (PropertyDescriptor prop in properties) row[prop.Name] = prop.GetValue(item) ?? DBNull.Value; table.Rows.Add(row); } return table; } }}

VB:

Imports System.DataImports System.CollectionsImports System.Reflection' ' IList 公共帮助类' Public Class IListHelper    '''     ''' Convert a DataGridView to DataTable    '''     '''     ''' 
Public Function ToDataTable(ByVal dgv As DataGridView) As DataTable Dim tb = New DataTable(dgv.Name) 'Add table column For Each col In dgv.Columns.OfType(Of DataGridViewColumn) tb.Columns.Add(col.HeaderText) Next 'Content For Each dgvrow In dgv.Rows.OfType(Of DataGridViewRow) Dim datarow = tb.NewRow() For Each col In dgv.Columns.OfType(Of DataGridViewColumn) datarow(col.Name) = Convert.ToString(dgvrow.Cells(col.Name).Value) Next tb.Rows.Add(datarow) Next Return tb End Function ' ' Convert a List{T} to a DataTable. ' Public Function ToDataTable(Of T)(ByVal items As List(Of T)) As DataTable Dim tb = New DataTable(GetType(T).Name) Dim props As PropertyInfo() = GetType(T).GetProperties(BindingFlags.Public OrElse BindingFlags.Instance) For Each prop As PropertyInfo In props 'Type T = GetCoreType(prop.PropertyType) tb.Columns.Add(prop.Name, GetCoreType(prop.PropertyType)) Next For Each item In items Dim values(props.Length - 1) As Object For i = 0 To props.Length - 1 values(i) = props(i).GetValue(item) Next tb.Rows.Add(values) Next Return tb End Function ' ' Determine of specified type Is nullable ' Public Function IsNullable(ByVal t As Type) As Boolean Return Not t.IsValueType 'Or (t.IsGenericType And t.GetGenericTypeDefinition() = GetType(Nullable())) End Function ' ' Return underlying type if type Is Nullable otherwise return the type ' Public Function GetCoreType(ByVal t As Type) As Type If t <> Nothing And IsNullable(t) Then If (Not t.IsValueType) Then Return t Else Return Nullable.GetUnderlyingType(t) End If Else Return t End If End FunctionEnd Class

转载地址:http://psmdi.baihongyu.com/

你可能感兴趣的文章
C++函数重载(3) - 函数重载中的const关键字
查看>>
C++函数重载(4) - 函数的返回类型
查看>>
C++函数重载(5) - 函数重载在类继承中的行为
查看>>
C++函数重载(6) - main函数重载
查看>>
C++内联函数
查看>>
C++内存分配 - malloc vs new 以及 delete vs free
查看>>
C++类与对象(1) - 基本概念
查看>>
C++类与对象(2) - class可以拥有自身类型的对象
查看>>
C++类与对象(3) - 空class&struct的大小
查看>>
C++静态成员(1) - 静态成员函数的特性
查看>>
C++静态成员(2) - 静态数据成员
查看>>
C++ this指针(1) - this介绍
查看>>
C++ this指针(2) - this指针的类型
查看>>
C++ this指针(3) - 删除this指针
查看>>
C++构造与析构(1) - 构造函数
查看>>
C++构造与析构(2) - 拷贝构造函数
查看>>
C++构造与析构(3) - 析构函数
查看>>
C++构造与析构(4) - 默认构造函数
查看>>
C++构造与析构(5) - 何时必须自定义拷贝构造函数
查看>>
C++构造与析构(7) - 数据成员的初始化
查看>>