Skip to content

Commit 11e19df

Browse files
committed
Add DataTable extensions ToJson and refactor anonimous class factory
1 parent 17989a2 commit 11e19df

8 files changed

Lines changed: 890 additions & 255 deletions

File tree

src/DomainCommonExtensions/CommonExtensions/SystemData/DataTableExtensions.cs

Lines changed: 86 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -16,11 +16,18 @@
1616

1717
#region U S A G E S
1818

19+
#if NET || NETSTANDARD2_0_OR_GREATER
20+
using DomainCommonExtensions.Helpers;
21+
using DomainCommonExtensions.Helpers.Internal.AnonymousSelect.Factory;
22+
#endif
23+
1924
using System;
2025
using System.Collections.Generic;
2126
using System.Data;
2227
using System.Linq;
2328

29+
// ReSharper disable RedundantArgumentDefaultValue
30+
2431
#endregion
2532

2633
namespace DomainCommonExtensions.CommonExtensions.SystemData
@@ -65,5 +72,84 @@ public static class DataTableExtensions
6572

6673
return list;
6774
}
75+
76+
/// <summary>
77+
/// System.Data.DataTable to generic list T.
78+
/// </summary>
79+
/// <param name="table">.</param>
80+
/// <param name="type">The type.</param>
81+
/// <param name="excludeProp">(Optional) The exclude property.</param>
82+
/// <returns>
83+
/// The given data converted to a List&lt;dynamic&gt;
84+
/// </returns>
85+
public static List<dynamic> ToList(this DataTable table, Type type, string excludeProp = "Item")
86+
{
87+
var list = new List<dynamic>();
88+
var typeProperties = type.GetProperties().Where(x => x.Name != excludeProp)
89+
.Select(propertyInfo => new
90+
{
91+
PropertyInfo = propertyInfo,
92+
Type = Nullable.GetUnderlyingType(propertyInfo.PropertyType) ?? propertyInfo.PropertyType
93+
}).ToList();
94+
95+
foreach (var row in table.Rows.Cast<DataRow>())
96+
{
97+
var obj = Activator.CreateInstance(type);
98+
foreach (var typeProperty in typeProperties)
99+
{
100+
var value = row[typeProperty.PropertyInfo.Name];
101+
var safeValue = value == null || DBNull.Value.Equals(value)
102+
? null
103+
: Convert.ChangeType(value, typeProperty.Type);
104+
105+
typeProperty.PropertyInfo.SetValue(obj, safeValue, null);
106+
}
107+
108+
list.Add(obj);
109+
}
110+
111+
return list;
112+
}
113+
114+
#if NET || NETSTANDARD2_0_OR_GREATER
115+
116+
/// <summary>
117+
/// A DataTable extension method that converts a table to a JSON.
118+
/// </summary>
119+
/// <typeparam name="T">Generic type parameter.</typeparam>
120+
/// <param name="table">The table to act on.</param>
121+
/// <returns>
122+
/// DataTable as a JSON string.
123+
/// </returns>
124+
public static string ToJson<T>(this DataTable table) where T : new()
125+
{
126+
return JsonObjectSerializer.ToString(table.ToList<T>());
127+
}
128+
129+
/// <summary>
130+
/// A DataTable extension method that converts a table to a JSON.
131+
/// </summary>
132+
/// <param name="table">The table to act on.</param>
133+
/// <returns>
134+
/// DataTable as a JSON string.
135+
/// </returns>
136+
public static string ToJson(this DataTable table)
137+
{
138+
var columnCount = table.Columns.Count;
139+
var types = new Type[columnCount];
140+
var names = new string[columnCount];
141+
142+
for (var i = 0; i < columnCount; i++)
143+
{
144+
types[i] = table.Columns[i].DataType;
145+
names[i] = table.Columns[i].ColumnName;
146+
}
147+
148+
var type = AnonymousClassFactory.CreateType(types, names, true);
149+
var list = table.ToList(type);
150+
151+
return JsonObjectSerializer.ToString(list);
152+
}
153+
#endif
68154
}
69155
}

src/DomainCommonExtensions/Helpers/Internal/AnonymousSelect/Factory/AnonymousClassFactory.cs

Lines changed: 95 additions & 255 deletions
Large diffs are not rendered by default.

0 commit comments

Comments
 (0)