11/* Report */
22
33using System ;
4- using System . Collections . Generic ;
54using System . Data ;
6- using System . Globalization ;
75using System . Linq ;
86using System . Text . Json ;
7+ using System . Globalization ;
8+ using System . Collections . Generic ;
9+ using System . Text . Json . Serialization ;
910
1011namespace PayrollEngine . Client . Scripting . Report ;
1112
@@ -1020,6 +1021,52 @@ public static implicit operator string(ExpressionBase function) =>
10201021
10211022#region Extensions
10221023
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+
10231070// duplicated in PayrollEngine.DataTableExtensions
10241071/// <summary>Data table extension methods</summary>
10251072public static class DataTableExtensions
@@ -1037,7 +1084,7 @@ public static void RemovePrimaryKey(this DataTable table)
10371084 /// <summary>Test for table column</summary>
10381085 /// <param name="table">The table</param>
10391086 /// <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 ) =>
10411088 table . Columns . Contains ( columnName ) ;
10421089
10431090 /// <summary>Add table column</summary>
@@ -1352,6 +1399,35 @@ public static int DeleteRows(this DataTable table, string filterExpression)
13521399 return deleteCount ;
13531400 }
13541401
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+
13551431 /// <summary>Get data table rows value</summary>
13561432 /// <param name="table">The data table</param>
13571433 /// <param name="column">The column name</param>
@@ -1411,6 +1487,36 @@ public static string Identifier(this DataRow dataRow) =>
14111487 public static ObjectStatus ObjectStatus ( this DataRow dataRow ) =>
14121488 GetEnumValue ( dataRow , "Status" , Scripting . ObjectStatus . Inactive ) ;
14131489
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+
14141520 /// <summary>Get data row enum value</summary>
14151521 /// <param name="dataRow">The data row</param>
14161522 /// <param name="column">The column name</param>
@@ -1667,6 +1773,26 @@ public static string GetLocalizedValue(this DataRow dataRow, string valueColumn,
16671773 return value ;
16681774 }
16691775
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+
16701796 /// <summary>Get data rows value</summary>
16711797 /// <param name="dataRows">The data rows</param>
16721798 /// <param name="column">The column name</param>
0 commit comments