1
1
/* Report */
2
2
3
3
using System ;
4
- using System . Collections . Generic ;
5
4
using System . Data ;
6
- using System . Globalization ;
7
5
using System . Linq ;
8
6
using System . Text . Json ;
7
+ using System . Globalization ;
8
+ using System . Collections . Generic ;
9
+ using System . Text . Json . Serialization ;
9
10
10
11
namespace PayrollEngine . Client . Scripting . Report ;
11
12
@@ -1020,6 +1021,52 @@ public static implicit operator string(ExpressionBase function) =>
1020
1021
1021
1022
#region Extensions
1022
1023
1024
+ // duplicated in PayrollEngine.DataTableExtensions
1025
+ /// <summary>Data set extension methods</summary>
1026
+ public static class DataSetExtensions
1027
+ {
1028
+ /// <summary>Test for data set rows</summary>
1029
+ /// <param name="dataSet">The system data set to convert</param>
1030
+ /// <returns>True if any row is available</returns>
1031
+ public static bool HasRows ( this DataSet dataSet )
1032
+ {
1033
+ if ( dataSet ? . Tables == null || dataSet . Tables . Count == 0 )
1034
+ {
1035
+ return false ;
1036
+ }
1037
+ return dataSet . Tables . Cast < DataTable > ( ) . Any ( table => table . Rows . Count > 0 ) ;
1038
+ }
1039
+
1040
+ /// <summary>Get data set table rows values as dictionary</summary>
1041
+ /// <param name="dataSet">The payroll data set to convert</param>
1042
+ /// <returns>The data table values as dictionary, key is table column name</returns>
1043
+ public static Dictionary < string , List < Dictionary < string , object > > > AsDictionary ( this DataSet dataSet )
1044
+ {
1045
+ var values = new Dictionary < string , List < Dictionary < string , object > > > ( ) ;
1046
+ foreach ( DataTable table in dataSet . Tables )
1047
+ {
1048
+ values . Add ( table . TableName , table . AsDictionary ( ) ) ;
1049
+ }
1050
+ return values ;
1051
+ }
1052
+
1053
+ /// <summary>Get data set as json</summary>
1054
+ /// <param name="dataSet">The payroll data set to convert</param>
1055
+ /// <param name="namingPolicy">Naming policy (default: camel case)</param>
1056
+ /// <param name="ignoreNull">Ignore null values (default: true)</param>
1057
+ public static string Json ( this DataSet dataSet , JsonNamingPolicy namingPolicy = null ,
1058
+ bool ignoreNull = true )
1059
+ {
1060
+ return JsonSerializer . Serialize ( AsDictionary ( dataSet ) , new JsonSerializerOptions
1061
+ {
1062
+ WriteIndented = true ,
1063
+ PropertyNamingPolicy = namingPolicy ?? JsonNamingPolicy . CamelCase ,
1064
+ DictionaryKeyPolicy = namingPolicy ?? JsonNamingPolicy . CamelCase ,
1065
+ DefaultIgnoreCondition = ignoreNull ? JsonIgnoreCondition . WhenWritingNull : default
1066
+ } ) ;
1067
+ }
1068
+ }
1069
+
1023
1070
// duplicated in PayrollEngine.DataTableExtensions
1024
1071
/// <summary>Data table extension methods</summary>
1025
1072
public static class DataTableExtensions
@@ -1037,7 +1084,7 @@ public static void RemovePrimaryKey(this DataTable table)
1037
1084
/// <summary>Test for table column</summary>
1038
1085
/// <param name="table">The table</param>
1039
1086
/// <param name="columnName">Name of the column</param>
1040
- public static bool ContainsColumn ( DataTable table , string columnName ) =>
1087
+ public static bool ContainsColumn ( this DataTable table , string columnName ) =>
1041
1088
table . Columns . Contains ( columnName ) ;
1042
1089
1043
1090
/// <summary>Add table column</summary>
@@ -1352,6 +1399,35 @@ public static int DeleteRows(this DataTable table, string filterExpression)
1352
1399
return deleteCount ;
1353
1400
}
1354
1401
1402
+ /// <summary>Get data table as dictionary</summary>
1403
+ /// <param name="dataTable">The data table</param>
1404
+ /// <returns>List of row dictionaries</returns>
1405
+ public static List < Dictionary < string , object > > AsDictionary ( this DataTable dataTable )
1406
+ {
1407
+ var values = new List < Dictionary < string , object > > ( ) ;
1408
+ foreach ( DataRow row in dataTable . AsEnumerable ( ) )
1409
+ {
1410
+ values . Add ( row . AsDictionary ( ) ) ;
1411
+ }
1412
+ return values ;
1413
+ }
1414
+
1415
+ /// <summary>Get data table as json</summary>
1416
+ /// <param name="dataTable">The data table</param>
1417
+ /// <param name="namingPolicy">Naming policy (default: camel case)</param>
1418
+ /// <param name="ignoreNull">Ignore null values (default: true)</param>
1419
+ public static string Json ( this DataTable dataTable , JsonNamingPolicy namingPolicy = null ,
1420
+ bool ignoreNull = true )
1421
+ {
1422
+ return JsonSerializer . Serialize ( AsDictionary ( dataTable ) , new JsonSerializerOptions
1423
+ {
1424
+ WriteIndented = true ,
1425
+ PropertyNamingPolicy = namingPolicy ?? JsonNamingPolicy . CamelCase ,
1426
+ DictionaryKeyPolicy = namingPolicy ?? JsonNamingPolicy . CamelCase ,
1427
+ DefaultIgnoreCondition = ignoreNull ? JsonIgnoreCondition . WhenWritingNull : default
1428
+ } ) ;
1429
+ }
1430
+
1355
1431
/// <summary>Get data table rows value</summary>
1356
1432
/// <param name="table">The data table</param>
1357
1433
/// <param name="column">The column name</param>
@@ -1411,6 +1487,36 @@ public static string Identifier(this DataRow dataRow) =>
1411
1487
public static ObjectStatus ObjectStatus ( this DataRow dataRow ) =>
1412
1488
GetEnumValue ( dataRow , "Status" , Scripting . ObjectStatus . Inactive ) ;
1413
1489
1490
+ /// <summary>Get data row values as dictionary</summary>
1491
+ /// <param name="dataRow">The data row</param>
1492
+ /// <returns>The data rows values as dictionary, key is the column name</returns>
1493
+ public static Dictionary < string , object > AsDictionary ( this DataRow dataRow )
1494
+ {
1495
+ var values = new Dictionary < string , object > ( ) ;
1496
+ foreach ( DataColumn column in dataRow . Table . Columns )
1497
+ {
1498
+ values . Add ( column . ColumnName , GetValue < object > ( dataRow , column . ColumnName ) ) ;
1499
+ }
1500
+ return values ;
1501
+ }
1502
+
1503
+ /// <summary>Get data row as json</summary>
1504
+ /// <param name="dataRow">The data row</param>
1505
+ /// <param name="namingPolicy">Naming policy (default: camel case)</param>
1506
+ /// <param name="ignoreNull">Ignore null values (default: true)</param>
1507
+ public static string Json ( this DataRow dataRow , JsonNamingPolicy namingPolicy = null ,
1508
+ bool ignoreNull = true )
1509
+ {
1510
+ ArgumentNullException . ThrowIfNull ( dataRow ) ;
1511
+ return JsonSerializer . Serialize ( AsDictionary ( dataRow ) , new JsonSerializerOptions
1512
+ {
1513
+ WriteIndented = true ,
1514
+ PropertyNamingPolicy = namingPolicy ?? JsonNamingPolicy . CamelCase ,
1515
+ DictionaryKeyPolicy = namingPolicy ?? JsonNamingPolicy . CamelCase ,
1516
+ DefaultIgnoreCondition = ignoreNull ? JsonIgnoreCondition . WhenWritingNull : default
1517
+ } ) ;
1518
+ }
1519
+
1414
1520
/// <summary>Get data row enum value</summary>
1415
1521
/// <param name="dataRow">The data row</param>
1416
1522
/// <param name="column">The column name</param>
@@ -1667,6 +1773,26 @@ public static string GetLocalizedValue(this DataRow dataRow, string valueColumn,
1667
1773
return value ;
1668
1774
}
1669
1775
1776
+ /// <summary>Get data row values</summary>
1777
+ /// <param name="dataRows">The data rows</param>
1778
+ /// <returns>The data rows values</returns>
1779
+ public static List < object > GetValues ( this IEnumerable < DataRow > dataRows )
1780
+ {
1781
+ if ( dataRows == null )
1782
+ {
1783
+ throw new ArgumentNullException ( nameof ( dataRows ) ) ;
1784
+ }
1785
+ var values = new List < object > ( ) ;
1786
+ foreach ( DataRow row in dataRows )
1787
+ {
1788
+ foreach ( DataColumn column in row . Table . Columns )
1789
+ {
1790
+ values . Add ( row [ column . ColumnName ] ) ;
1791
+ }
1792
+ }
1793
+ return values ;
1794
+ }
1795
+
1670
1796
/// <summary>Get data rows value</summary>
1671
1797
/// <param name="dataRows">The data rows</param>
1672
1798
/// <param name="column">The column name</param>
0 commit comments