11using System . Collections . Generic ;
2+ using FluentAssertions ;
23using NFluent ;
34using Xunit ;
45
5- namespace System . Linq . Dynamic . Core . Tests
6+ namespace System . Linq . Dynamic . Core . Tests ;
7+
8+ public partial class QueryableTests
69{
7- public partial class QueryableTests
10+ [ Fact ]
11+ public void GroupByMany_Dynamic_LambdaExpressions ( )
12+ {
13+ var lst = new List < Tuple < int , int , int > >
14+ {
15+ new ( 1 , 1 , 1 ) ,
16+ new ( 1 , 1 , 2 ) ,
17+ new ( 1 , 1 , 3 ) ,
18+ new ( 2 , 2 , 4 ) ,
19+ new ( 2 , 2 , 5 ) ,
20+ new ( 2 , 2 , 6 ) ,
21+ new ( 2 , 3 , 7 )
22+ } ;
23+
24+ var sel = lst . GroupByMany ( x => x . Item1 , x => x . Item2 ) . ToArray ( ) ;
25+
26+ Assert . Equal ( 2 , sel . Length ) ;
27+ Assert . Single ( sel . First ( ) . Subgroups ) ;
28+ Assert . Equal ( 2 , sel . Skip ( 1 ) . First ( ) . Subgroups . Count ( ) ) ;
29+ }
30+
31+ [ Fact ]
32+ public void GroupByMany_Dynamic_StringExpressions ( )
833 {
9- [ Fact ]
10- public void GroupByMany_Dynamic_LambdaExpressions ( )
34+ var lst = new List < Tuple < int , int , int > >
1135 {
12- var lst = new List < Tuple < int , int , int > >
13- {
14- new Tuple < int , int , int > ( 1 , 1 , 1 ) ,
15- new Tuple < int , int , int > ( 1 , 1 , 2 ) ,
16- new Tuple < int , int , int > ( 1 , 1 , 3 ) ,
17- new Tuple < int , int , int > ( 2 , 2 , 4 ) ,
18- new Tuple < int , int , int > ( 2 , 2 , 5 ) ,
19- new Tuple < int , int , int > ( 2 , 2 , 6 ) ,
20- new Tuple < int , int , int > ( 2 , 3 , 7 )
21- } ;
22-
23- var sel = lst . AsQueryable ( ) . GroupByMany ( x => x . Item1 , x => x . Item2 ) ;
24-
25- Assert . Equal ( 2 , sel . Count ( ) ) ;
26- Assert . Single ( sel . First ( ) . Subgroups ) ;
27- Assert . Equal ( 2 , sel . Skip ( 1 ) . First ( ) . Subgroups . Count ( ) ) ;
28- }
29-
30- [ Fact ]
31- public void GroupByMany_Dynamic_StringExpressions ( )
36+ new ( 1 , 1 , 1 ) ,
37+ new ( 1 , 1 , 2 ) ,
38+ new ( 1 , 1 , 3 ) ,
39+ new ( 2 , 2 , 4 ) ,
40+ new ( 2 , 2 , 5 ) ,
41+ new ( 2 , 2 , 6 ) ,
42+ new ( 2 , 3 , 7 )
43+ } ;
44+
45+ var sel = lst . GroupByMany ( "Item1" , "Item2" ) . ToList ( ) ;
46+
47+ Check . That ( sel . Count ) . Equals ( 2 ) ;
48+
49+ var firstGroupResult = sel . First ( ) ;
50+ Check . That ( firstGroupResult . ToString ( ) ) . Equals ( "1 (3)" ) ;
51+ Check . That ( firstGroupResult . Subgroups . Count ( ) ) . Equals ( 1 ) ;
52+
53+ var skippedGroupResult = sel . Skip ( 1 ) . First ( ) ;
54+ Check . That ( skippedGroupResult . ToString ( ) ) . Equals ( "2 (4)" ) ;
55+ Check . That ( skippedGroupResult . Subgroups . Count ( ) ) . Equals ( 2 ) ;
56+ }
57+
58+ [ Fact ]
59+ public void GroupByMany_Dynamic_CompositeKey ( )
60+ {
61+ // Arrange
62+ var data = new [ ]
3263 {
33- var lst = new List < Tuple < int , int , int > >
34- {
35- new Tuple < int , int , int > ( 1 , 1 , 1 ) ,
36- new Tuple < int , int , int > ( 1 , 1 , 2 ) ,
37- new Tuple < int , int , int > ( 1 , 1 , 3 ) ,
38- new Tuple < int , int , int > ( 2 , 2 , 4 ) ,
39- new Tuple < int , int , int > ( 2 , 2 , 5 ) ,
40- new Tuple < int , int , int > ( 2 , 2 , 6 ) ,
41- new Tuple < int , int , int > ( 2 , 3 , 7 )
42- } ;
43-
44- var sel = lst . AsQueryable ( ) . GroupByMany ( "Item1" , "Item2" ) . ToList ( ) ;
45-
46- Check . That ( sel . Count ) . Equals ( 2 ) ;
47-
48- var firstGroupResult = sel . First ( ) ;
49- Check . That ( firstGroupResult . ToString ( ) ) . Equals ( "1 (3)" ) ;
50- Check . That ( firstGroupResult . Subgroups . Count ( ) ) . Equals ( 1 ) ;
51-
52- var skippedGroupResult = sel . Skip ( 1 ) . First ( ) ;
53- Check . That ( skippedGroupResult . ToString ( ) ) . Equals ( "2 (4)" ) ;
54- Check . That ( skippedGroupResult . Subgroups . Count ( ) ) . Equals ( 2 ) ;
55- }
64+ new { MachineId = 1 , Machine = new { Id = 1 , Name = "A" } } ,
65+ new { MachineId = 1 , Machine = new { Id = 1 , Name = "A" } } ,
66+ new { MachineId = 2 , Machine = new { Id = 2 , Name = "B" } }
67+ } ;
68+
69+ // Act
70+ var normalResult = data
71+ . GroupByMany ( d => new { d . MachineId , d . Machine . Name } , a => a . Machine . Id )
72+ . Select ( x => x . ToString ( ) )
73+ . ToList ( ) ;
74+ var result = data
75+ . GroupByMany ( "new (MachineId, Machine.Name)" , "Machine.Id" )
76+ . Select ( x => x . ToString ( ) )
77+ . ToList ( ) ;
78+
79+ // Assert
80+ result . Should ( ) . BeEquivalentTo ( normalResult ) ;
5681 }
57- }
82+ }
0 commit comments