-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathDataTableExtensions.cs
More file actions
64 lines (56 loc) · 1.75 KB
/
Copy pathDataTableExtensions.cs
File metadata and controls
64 lines (56 loc) · 1.75 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
namespace System.Data;
/// <summary>Provides extension methods for <see cref="T:System.Data.DataTable" />.</summary>
public static class DataTableExtensions
{
public static void ClearAndDispose(this DataTable? dataTable)
{
if (dataTable is not null)
{
dataTable.Clear();
dataTable.Dispose();
}
}
public static DataRow? FindRow(this DataTable dataTable, string columnName, Predicate<object?> match)
{
ArgumentNullException.ThrowIfNull(dataTable);
ArgumentException.ThrowIfNullOrEmpty(columnName);
ArgumentNullException.ThrowIfNull(match);
if (dataTable.Rows.Count == 0)
{
return null;
}
foreach (DataRow row in dataTable.Rows)
{
if (match(row[columnName]))
{
return row;
}
}
return null;
}
public static int GetChangeCount(this DataTable dataTable)
{
ArgumentNullException.ThrowIfNull(dataTable);
int changes = 0;
foreach (DataRow row in dataTable.Rows)
{
if ((row.RowState & (DataRowState.Modified | DataRowState.Deleted | DataRowState.Added)) != 0)
changes++;
}
return changes;
}
public static bool HasChanges(this DataTable dataTable)
{
ArgumentNullException.ThrowIfNull(dataTable);
foreach (DataRow row in dataTable.Rows)
{
if ((row.RowState & (DataRowState.Modified | DataRowState.Deleted | DataRowState.Added)) != 0)
return true;
}
return false;
}
public static bool IsNullOrEmpty(this DataTable? dataTable)
{
return dataTable is null || dataTable.Rows.Count == 0;
}
}