1- using FluentAssertions ;
1+ using System . Data ;
2+ using FluentAssertions ;
23using Xunit ;
34
45namespace System . Linq . Dynamic . Core . Tests ;
@@ -14,8 +15,16 @@ public class SalesData
1415 public string Sales { get ; set ; } = null ! ;
1516 }
1617
18+ public class GroupedSalesData
19+ {
20+ public string Region { get ; set ; } = null ! ;
21+ public string ? Product { get ; set ; }
22+ public int TotalSales { get ; set ; }
23+ public int GroupLevel { get ; set ; }
24+ }
25+
1726 [ Fact ]
18- public void DynamicGetMemberBinder_Test1 ( )
27+ public void DynamicGetMemberBinder_SelectOnArrayWithComplexObjects ( )
1928 {
2029 // Arrange
2130 var rows = new SalesData [ ]
@@ -44,7 +53,100 @@ public void DynamicGetMemberBinder_Test1()
4453 }
4554
4655 [ Fact ]
47- public void DynamicGetMemberBinder_Test2 ( )
56+ public void DynamicGetMemberBinder_SelectTypeOnArrayWithComplexObjects ( )
57+ {
58+ // Arrange
59+ var rows = new SalesData [ ]
60+ {
61+ new ( ) { Region = "North" , Product = "Widget" , Sales = "100" } ,
62+ new ( ) { Region = "North" , Product = "Gadget" , Sales = "150" } ,
63+ new ( ) { Region = "South" , Product = "Widget" , Sales = "200" } ,
64+ new ( ) { Region = "South" , Product = "Gadget" , Sales = "100" } ,
65+ new ( ) { Region = "North" , Product = "Widget" , Sales = "50" }
66+ } . AsQueryable ( ) ;
67+
68+ // Act
69+ var grouping1 = rows
70+ . GroupBy ( "new (Region, Product)" )
71+ . Select < GroupedSalesData > ( "new (Key.Region as Region, Key.Product as Product, Sum(Convert.ToInt32(Sales)) as TotalSales, 0 as GroupLevel)" ) ;
72+
73+ var grouping2 = rows
74+ . GroupBy ( "Region" )
75+ . Select < GroupedSalesData > ( "new (Key as Region, null as Product, Sum(Convert.ToInt32(Sales)) as TotalSales, 1 as GroupLevel)" ) ;
76+
77+ var combined = grouping1 . Concat ( grouping2 ) . AsQueryable ( ) ;
78+ var ordered = combined . OrderBy ( "Product" ) . ToDynamicList ( ) ;
79+
80+ // Assert
81+ ordered . Should ( ) . HaveCount ( 6 ) ;
82+ }
83+
84+ [ Fact ]
85+ public void DynamicGetMemberBinder_SelectOnDataTable ( )
86+ {
87+ // Arrange
88+ var dataTable = new DataTable ( ) ;
89+ dataTable . Columns . Add ( "Region" , typeof ( string ) ) ;
90+ dataTable . Columns . Add ( "Product" , typeof ( string ) ) ;
91+ dataTable . Columns . Add ( "Sales" , typeof ( int ) ) ;
92+
93+ dataTable . Rows . Add ( "North" , "Apples" , 100 ) ;
94+ dataTable . Rows . Add ( "North" , "Oranges" , 150 ) ;
95+ dataTable . Rows . Add ( "South" , "Apples" , 200 ) ;
96+ dataTable . Rows . Add ( "South" , "Oranges" , 250 ) ;
97+
98+ var rows = dataTable . Rows . Cast < DataRow > ( ) . AsQueryable ( ) ;
99+
100+ // Act
101+ var grouping1 = rows
102+ . GroupBy ( "new (Region, Product)" )
103+ . Select ( "new (Key.Region as Region, Key.Product as Product, Sum(Convert.ToInt32(Sales)) as TotalSales, 0 as GroupLevel)" ) ;
104+
105+ var grouping2 = rows
106+ . GroupBy ( "Region" )
107+ . Select ( "new (Key as Region, null as Product, Sum(Convert.ToInt32(Sales)) as TotalSales, 1 as GroupLevel)" ) ;
108+
109+ var combined = grouping1 . ToDynamicArray ( ) . Concat ( grouping2 . ToDynamicArray ( ) ) . AsQueryable ( ) ;
110+ var ordered = combined . OrderBy ( "Product" ) . ToDynamicList ( ) ;
111+
112+ // Assert
113+ ordered . Should ( ) . HaveCount ( 6 ) ;
114+ }
115+
116+ [ Fact ]
117+ public void DynamicGetMemberBinder_SelectTypeOnDataTable ( )
118+ {
119+ // Arrange
120+ var dataTable = new DataTable ( ) ;
121+ dataTable . Columns . Add ( "Region" , typeof ( string ) ) ;
122+ dataTable . Columns . Add ( "Product" , typeof ( string ) ) ;
123+ dataTable . Columns . Add ( "Sales" , typeof ( int ) ) ;
124+
125+ dataTable . Rows . Add ( "North" , "Apples" , 100 ) ;
126+ dataTable . Rows . Add ( "North" , "Oranges" , 150 ) ;
127+ dataTable . Rows . Add ( "South" , "Apples" , 200 ) ;
128+ dataTable . Rows . Add ( "South" , "Oranges" , 250 ) ;
129+
130+ var rows = dataTable . Rows . Cast < DataRow > ( ) . AsQueryable ( ) ;
131+
132+ // Act
133+ var grouping1 = rows
134+ . GroupBy ( "new (Region, Product)" )
135+ . Select < GroupedSalesData > ( "new (Key.Region as Region, Key.Product as Product, Sum(Convert.ToInt32(Sales)) as TotalSales, 0 as GroupLevel)" ) ;
136+
137+ var grouping2 = rows
138+ . GroupBy ( "Region" )
139+ . Select < GroupedSalesData > ( "new (Key as Region, null as Product, Sum(Convert.ToInt32(Sales)) as TotalSales, 1 as GroupLevel)" ) ;
140+
141+ var combined = grouping1 . ToDynamicArray ( ) . Concat ( grouping2 . ToDynamicArray ( ) ) . AsQueryable ( ) ;
142+ var ordered = combined . OrderBy ( "Product" ) . ToDynamicList ( ) ;
143+
144+ // Assert
145+ ordered . Should ( ) . HaveCount ( 6 ) ;
146+ }
147+
148+ [ Fact ]
149+ public void DynamicGetMemberBinder_SelectOnArrayWithIntegers ( )
48150 {
49151 // Arrange
50152 var dynamicData = new [ ] { 1 , 2 }
0 commit comments