src/Library/NetPro.Dapper/Expressions/DataTableExtensions.cs
(开头部分) 5KB这里只显示每个文件的开头 60 行。登录后可以解锁完整代码。
using Dapper;
using Microsoft.Data.SqlClient;
using System;
using System.Collections.Generic;
using System.Data;
using System.Linq;
using System.Reflection;
namespace NetPro.Dapper.Expressions
{
public static class DataTableExtensions
{
/// <summary>
/// List 转换成Datatable
/// </summary>
/// <typeparam name="T"></typeparam>
/// <param name="t"></param>
/// <returns></returns>
public delegate object[] CreateRowDelegate<T>(T t);
public static DataTable ToDataTable<T>(this IEnumerable<T> varlist, CreateRowDelegate<T> fn, string[] fields = null)
{
//存表的列名
DataTable dtReturn = new DataTable();
// 访问属性元素
PropertyInfo[] oProps = null;
// 判断属性元素大于0就遍历
foreach (T rec in varlist)
{
// 用反射来获取属性名,创建表,只执行第一次
if (oProps == null)
{
//得到公有属性
oProps = rec.GetType().GetProperties();
//遍历属性中的数据
foreach (PropertyInfo pi in oProps)
{
if (fields != null && string.IsNullOrWhiteSpace(fields.FirstOrDefault(s => s.ToLower().Equals(pi.Name.ToLower()))))
{
continue;
}
//获取属性的名称与类型
Type colType = pi.PropertyType;
if ((colType.IsGenericType) && (colType.GetGenericTypeDefinition() == typeof(Nullable<>)))
{
colType = colType.GetGenericArguments()[0];
}
dtReturn.Columns.Add(new DataColumn(pi.Name, colType));
}
后面还有 87 行代码,解锁后查看完整代码
24 小时内免费解锁 3 个项目,之后 1 算力/个。 规则说明
AI 解读 AI
登录后可用,每次 10 算力,解读结果公开显示在下面。
还没有人解读过这个文件。