Skip to content

Commit

Permalink
list.ToDataTable支持可空类型
Browse files Browse the repository at this point in the history
  • Loading branch information
ldqk committed Mar 22, 2024
1 parent 37324a3 commit 84dc6e7
Show file tree
Hide file tree
Showing 7 changed files with 23 additions and 9 deletions.
20 changes: 17 additions & 3 deletions Masuit.Tools.Abstractions/Database/DataTableHelper.cs
Original file line number Diff line number Diff line change
Expand Up @@ -86,8 +86,22 @@ public static DataTable ToDataTable<T>(this IList<T> list, string tableName = nu
{
foreach (var property in typeof(T).GetProperties())
{
// 添加表头列,列名为属性名
result.Columns.Add(property.Name, property.PropertyType);
var underlyingType = Nullable.GetUnderlyingType(property.PropertyType);
if (underlyingType != null)
{
// 如果该类型具有可为空性
var column = new DataColumn(property.Name, underlyingType);
column.AllowDBNull = true;
// 添加表头列,列名为属性名
result.Columns.Add(column);
}
else
{
// 没有可为空性
var column = new DataColumn(property.Name, property.PropertyType);
// 添加表头列,列名为属性名
result.Columns.Add(column);
}
}
return result;
}
Expand Down Expand Up @@ -232,4 +246,4 @@ public static DataTable GetTableFromRows(this DataRow[] rows)
return dt;
}
}
}
}
2 changes: 1 addition & 1 deletion Masuit.Tools.Abstractions/Masuit.Tools.Abstractions.csproj
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@
<TargetFrameworks>netstandard2.0;netstandard2.1;net461;net5;net6;net7;net8</TargetFrameworks>
<LangVersion>latest</LangVersion>
<AllowUnsafeBlocks>true</AllowUnsafeBlocks>
<Version>2.6.9.7</Version>
<Version>2.6.9.8</Version>
<Authors>懒得勤快</Authors>
<Description>全龄段友好的C#万能工具库,码数吐司库,不管你是菜鸟新手还是骨灰级玩家都能轻松上手,Masuit.Tools基础公共库(适用于.NET4.6.1/.NET Standard2.0及以上项目),包含一些常用的操作类,大都是静态类,加密解密,反射操作,Excel简单导出,权重随机筛选算法,分布式短id,表达式树,linq扩展,文件压缩,多线程下载和FTP客户端,硬件信息,字符串扩展方法,日期时间扩展操作,中国农历,大文件拷贝,图像裁剪,验证码,断点续传,集合扩展等常用封装。
官网教程:https://tools.masuit.org
Expand Down
2 changes: 1 addition & 1 deletion Masuit.Tools.AspNetCore/Masuit.Tools.AspNetCore.csproj
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,7 @@
<Product>Masuit.Tools.AspNetCore</Product>
<PackageId>Masuit.Tools.AspNetCore</PackageId>
<LangVersion>latest</LangVersion>
<Version>1.2.9.7</Version>
<Version>1.2.9.8</Version>
<RepositoryType></RepositoryType>
<GeneratePackageOnBuild>True</GeneratePackageOnBuild>
<FileVersion>1.1.9</FileVersion>
Expand Down
2 changes: 1 addition & 1 deletion Masuit.Tools.Core/Masuit.Tools.Core.csproj
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@
官网教程:https://tools.masuit.org
github:https://github.com/ldqk/Masuit.Tools
</Description>
<Version>2.6.9.7</Version>
<Version>2.6.9.8</Version>
<Copyright>Copyright © 懒得勤快</Copyright>
<PackageProjectUrl>https://github.com/ldqk/Masuit.Tools</PackageProjectUrl>
<PackageTags>Masuit.Tools,工具库,Utility,Crypt,Extensions</PackageTags>
Expand Down
2 changes: 1 addition & 1 deletion Masuit.Tools.Excel/Masuit.Tools.Excel.csproj
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@
<TargetFramework>netstandard2.0</TargetFramework>
<LangVersion>latest</LangVersion>
<AllowUnsafeBlocks>true</AllowUnsafeBlocks>
<Version>1.2.9.7</Version>
<Version>1.2.9.8</Version>
<Authors>懒得勤快</Authors>
<Description>Masuit.Tools.Excel导出库,支持一些简单数据的导出,支持图片列</Description>
<Copyright>懒得勤快</Copyright>
Expand Down
2 changes: 1 addition & 1 deletion Masuit.Tools.Net45/package.nuspec
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@
<package>
<metadata>
<id>Masuit.Tools.Net45</id>
<version>2.6.9.7</version>
<version>2.6.9.8</version>
<title>Masuit.Tools</title>
<authors>懒得勤快</authors>
<owners>masuit.com</owners>
Expand Down
2 changes: 1 addition & 1 deletion Masuit.Tools/package.nuspec
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@
<package>
<metadata>
<id>Masuit.Tools.Net</id>
<version>2.6.9.7</version>
<version>2.6.9.8</version>
<title>Masuit.Tools</title>
<authors>懒得勤快</authors>
<owners>masuit.com</owners>
Expand Down

0 comments on commit 84dc6e7

Please sign in to comment.