zoukankan      html  css  js  c++  java
  • DataTable转换成List

    using System;
    using System.Collections.Generic;
    using System.Data;
    using System.Reflection;
    using System.Text;

    public class Helper<T> where T : new()
    {
    /// <summary>
    /// datatable转list
    /// </summary>
    /// <param name="dt"></param>
    /// <returns></returns>
    public static List<T> ConvertToModel(DataTable dt)
    {

    List<T> ts = new List<T>();// 定义集合
    Type type = typeof(T); // 获得此模型的类型
    string tempName = "";
    foreach (DataRow dr in dt.Rows)
    {
    T t = new T();
    PropertyInfo[] propertys = t.GetType().GetProperties();// 获得此模型的公共属性
    foreach (PropertyInfo pi in propertys)
    {
    tempName = pi.Name;
    if (dt.Columns.Contains(tempName))
    {
    if (!pi.CanWrite) continue;
    object value = dr[tempName];
    if (value != DBNull.Value)
    pi.SetValue(t, value, null);
    }
    }
    ts.Add(t);
    }
    return ts;
    }


    }

  • 相关阅读:
    NSString拼接字符串
    2020/4/26
    2020/4/25
    2020/4/24
    2020/4/22
    2020/4/22
    2020/4/20
    2020/4/19
    2020/4/18
    2020/4/17
  • 原文地址:https://www.cnblogs.com/2260827114com/p/7121740.html
Copyright © 2011-2022 走看看