src/Library/NetPro.Utility/Extenisons/Extensions.Collection.cs
(开头部分) 3KB这里只显示每个文件的开头 60 行。登录后可以解锁完整代码。
using System.Collections.Generic;
using System.Data;
using System.Linq;
using System.Reflection;
namespace System.NetPro
{
/// <summary>
/// 集合类型扩展方法
/// </summary>
public static partial class Extensions
{
/// <summary>
/// 数据转为DataTable
/// </summary>
/// <typeparam name="T"></typeparam>
/// <param name="collection"></param>
/// <returns></returns>
public static DataTable ToDataTable<T>(this IEnumerable<T> collection) where T : class, new()
{
var tb = new DataTable(typeof(T).Name);
if (collection == null || !collection.Any())
return tb;
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 collection)
{
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;
}
/// <summary>
/// DataTable数据转为List
/// </summary>
/// <typeparam name="T"></typeparam>
/// <param name="source"></param>
/// <returns></returns>
public static List<T> ToList<T>(this DataTable source) where T : class, new()
{
List<T> list = new List<T>();
if (source == null || source.Rows.Count == 0)
return list;
//获得此模型的类型
Type type = typeof(T);
string tempName = "";
后面还有 56 行代码,解锁后查看完整代码
24 小时内免费解锁 3 个项目,之后 1 算力/个。 规则说明
AI 解读 AI
登录后可用,每次 10 算力,解读结果公开显示在下面。
还没有人解读过这个文件。