Repository navigation
Expand file tree
/
Copy pathDataTableExtensions.cs
More file actions
99 lines (83 loc) · 3.24 KB
/
Copy pathDataTableExtensions.cs
File metadata and controls
99 lines (83 loc) · 3.24 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
using System;
using System.Collections.Generic;
using System.Data;
using System.Linq;
using System.Linq.Expressions;
using System.Reflection;
using BenchmarkDotNet.Attributes;
using BenchmarkDotNet.Running;
using Bogus;
namespace DataTableFromList;
public static class DataTableExtensions
{
public static DataTable ToDataTableUsingManualInitialization(this List<Student> students)
{
var table = new DataTable(nameof(Student));
// Define columns
table.Columns.Add(nameof(Student.Id), typeof(int));
table.Columns.Add(nameof(Student.FirstName), typeof(string));
table.Columns.Add(nameof(Student.LastName), typeof(string));
table.Columns.Add(nameof(Student.DateOfBirth), typeof(DateTime));
table.Columns.Add(nameof(Student.IsActive), typeof(bool));
table.Columns.Add(nameof(Student.Balance), typeof(decimal));
table.Columns.Add(nameof(Student.Email), typeof(string));
table.Columns.Add(nameof(Student.PhoneNumber), typeof(string));
table.Columns.Add(nameof(Student.Company), typeof(string));
table.Columns.Add(nameof(Student.Latitude), typeof(double));
table.Columns.Add(nameof(Student.Longitude), typeof(double));
// Populate rows
foreach (var student in students)
{
table.Rows.Add(
student.Id,
student.FirstName,
student.LastName,
student.DateOfBirth,
student.IsActive,
student.Balance,
student.Email,
student.PhoneNumber,
student.Company,
student.Latitude,
student.Longitude
);
}
return table;
}
public static DataTable ToDataTableWithExpressions<T>(this IEnumerable<T> collection)
{
var table = new DataTable();
if (!collection.Any()) return table;
List<(string name, Type type)> properties = TypeReflectionCache.GetOrCreatePropertyDetails<T>();
foreach (var prop in properties)
{
table.Columns.Add(prop.name, prop.type);
}
var convertToRowArray = TypeReflectionCache.GetOrCreateObjectArrayConverter<T>();
foreach (var item in collection)
{
table.Rows.Add(convertToRowArray(item));
}
return table;
}
public static DataTable ToDataTableUsingReflection<T>(this List<T> items)
{
var table = new DataTable(typeof(T).Name);
// Get properties of T
var properties = typeof(T).GetProperties(BindingFlags.Public | BindingFlags.Instance)
.Where(p => p.CanRead)
.ToList();
// Create columns from property names
foreach (var prop in properties)
{
table.Columns.Add(prop.Name, Nullable.GetUnderlyingType(prop.PropertyType) ?? prop.PropertyType);
}
// Add rows to DataTable
foreach (var item in items)
{
var values = properties.Select(prop => prop.GetValue(item, null)).ToArray();
table.Rows.Add(values);
}
return table;
}
}