Skip to content

Commit

Permalink
list可空类型转DataTable时bug修复(#2)
Browse files Browse the repository at this point in the history
  • Loading branch information
xucongli1989 committed Jul 21, 2017
1 parent 257a86e commit d2de283
Show file tree
Hide file tree
Showing 5 changed files with 73 additions and 3 deletions.
23 changes: 23 additions & 0 deletions UnitTest/DataSource/DataTableHelper.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,23 @@
using Microsoft.VisualStudio.TestTools.UnitTesting;
using System;
using System.Collections.Generic;

namespace UnitTest.DataSource
{
[TestClass]
public class DataTableHelper
{
[TestMethod]
public void ToDataTable()
{
var lst = new List<TestEntity.UserModel>();
var model = new TestEntity.UserModel();
model.ID = 100;
model.Age = null;
model.Name = "Test";
lst.Add(model);
var dt = XCLNetTools.DataSource.DataTableHelper.ToDataTable(lst);
Assert.IsTrue(null != dt && dt.Rows.Count == 1 && dt.Rows[0]["ID"].ToString() == "100" && dt.Rows[0]["Age"] == DBNull.Value && dt.Rows[0]["Name"].ToString() == "Test");
}
}
}
26 changes: 26 additions & 0 deletions UnitTest/Generic/ListHelper.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,26 @@
using Microsoft.VisualStudio.TestTools.UnitTesting;
using System;
using System.Data;

namespace UnitTest.Generic
{
[TestClass]
public class ListHelper
{
[TestMethod]
public void DataTableToList()
{
var dt = new DataTable();
dt.Columns.Add("ID", typeof(int));
dt.Columns.Add("Age", typeof(int));
dt.Columns.Add("Name", typeof(string));
var dr = dt.NewRow();
dr["ID"] = 100;
dr["Age"] = DBNull.Value;
dr["Name"] = "Test";
dt.Rows.Add(dr);
var lst = XCLNetTools.Generic.ListHelper.DataTableToList<TestEntity.UserModel>(dt);
Assert.IsTrue(null != lst && lst.Count == 1 && lst[0].ID == 100 && lst[0].Age == null && lst[0].Name == "Test");
}
}
}
14 changes: 14 additions & 0 deletions UnitTest/TestEntity/UserModel.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,14 @@
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;

namespace UnitTest.TestEntity
{
public class UserModel
{
public int ID { get; set; }
public int? Age { get; set; }
public string Name { get; set; }
}
}
4 changes: 4 additions & 0 deletions UnitTest/UnitTest.csproj
Original file line number Diff line number Diff line change
Expand Up @@ -39,6 +39,7 @@
<Reference Include="System.Core">
<RequiredTargetFramework>3.5</RequiredTargetFramework>
</Reference>
<Reference Include="System.Data" />
</ItemGroup>
<Choose>
<When Condition="('$(VisualStudioVersion)' == '10.0' or '$(VisualStudioVersion)' == '') and '$(TargetFrameworkVersion)' == 'v3.5'">
Expand All @@ -56,8 +57,11 @@
</Choose>
<ItemGroup>
<Compile Include="Common\SwitchControl.cs" />
<Compile Include="DataSource\DataTableHelper.cs" />
<Compile Include="Generic\ListHelper.cs" />
<Compile Include="Properties\AssemblyInfo.cs" />
<Compile Include="StringHander\DateHelper.cs" />
<Compile Include="TestEntity\UserModel.cs" />
</ItemGroup>
<ItemGroup>
<ProjectReference Include="..\XCLNetTools\XCLNetTools.csproj">
Expand Down
9 changes: 6 additions & 3 deletions XCLNetTools/DataSource/DataTableHelper.cs
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
using System.Collections.Generic;
using System;
using System.Collections.Generic;
using System.Data;

namespace XCLNetTools.DataSource
Expand Down Expand Up @@ -66,10 +67,12 @@ public static DataTable ToDataTable<T>(IList<T> data)
{
System.ComponentModel.PropertyDescriptorCollection properties = System.ComponentModel.TypeDescriptor.GetProperties(typeof(T));
DataTable dt = new DataTable();
Type nullableType;
for (int i = 0; i < properties.Count; i++)
{
System.ComponentModel.PropertyDescriptor property = properties[i];
dt.Columns.Add(property.Name, property.PropertyType);
var property = properties[i];
nullableType = Nullable.GetUnderlyingType(property.PropertyType);
dt.Columns.Add(property.Name, null == nullableType ? property.PropertyType : nullableType);
}
object[] values = new object[properties.Count];
foreach (T item in data)
Expand Down

0 comments on commit d2de283

Please sign in to comment.