@@ -285,19 +285,19 @@ public void SelectTupleKeyCountOfOrderLines()
285
285
group o by o . OrderDate
286
286
into g
287
287
select new
288
- {
289
- g . Key ,
290
- Count = g . SelectMany ( x => x . OrderLines ) . Count ( )
291
- } ) . ToList ( ) ;
288
+ {
289
+ g . Key ,
290
+ Count = g . SelectMany ( x => x . OrderLines ) . Count ( )
291
+ } ) . ToList ( ) ;
292
292
293
293
var query = ( from o in db . Orders
294
294
group o by o . OrderDate
295
295
into g
296
296
select new
297
- {
298
- g . Key ,
299
- Count = g . SelectMany ( x => x . OrderLines ) . Count ( )
300
- } ) . ToList ( ) ;
297
+ {
298
+ g . Key ,
299
+ Count = g . SelectMany ( x => x . OrderLines ) . Count ( )
300
+ } ) . ToList ( ) ;
301
301
302
302
Assert . That ( query . Count , Is . EqualTo ( 481 ) ) ;
303
303
Assert . That ( query , Is . EquivalentTo ( list ) ) ;
@@ -333,9 +333,9 @@ public void GroupByAndTake2()
333
333
{
334
334
//NH-2566
335
335
var results = ( from o in db . Orders
336
- group o by o . Customer
337
- into g
338
- select g . Key . CustomerId )
336
+ group o by o . Customer
337
+ into g
338
+ select g . Key . CustomerId )
339
339
. OrderBy ( customerId => customerId )
340
340
. Skip ( 10 )
341
341
. Take ( 10 )
@@ -418,6 +418,95 @@ public void SelectSingleOrDefaultElementFromProductsGroupedByUnitPrice()
418
418
Assert . That ( result . Count , Is . EqualTo ( 1 ) ) ;
419
419
}
420
420
421
+ [ Test ]
422
+ public void ProjectingCountWithPredicate ( )
423
+ {
424
+ var result = db . Products
425
+ . GroupBy ( x => x . Supplier . CompanyName )
426
+ . Select ( x => new { x . Key , Count = x . Count ( y => y . UnitPrice == 9.50M ) } )
427
+ . OrderByDescending ( x => x . Key )
428
+ . First ( ) ;
429
+
430
+ Assert . That ( result . Key , Is . EqualTo ( "Zaanse Snoepfabriek" ) ) ;
431
+ Assert . That ( result . Count , Is . EqualTo ( 1 ) ) ;
432
+ }
433
+
434
+ [ Test ]
435
+ public void FilteredByCountWithPredicate ( )
436
+ {
437
+ var result = db . Products
438
+ . GroupBy ( x => x . Supplier . CompanyName )
439
+ . Where ( x => x . Count ( y => y . UnitPrice == 12.75M ) == 1 )
440
+ . Select ( x => new { x . Key , Count = x . Count ( ) } )
441
+ . First ( ) ;
442
+
443
+ Assert . That ( result . Key , Is . EqualTo ( "Zaanse Snoepfabriek" ) ) ;
444
+ Assert . That ( result . Count , Is . EqualTo ( 2 ) ) ;
445
+ }
446
+
447
+ [ Test ]
448
+ public void FilteredByCountFromSubQuery ( )
449
+ {
450
+ //Not really an aggregate filter, but included to ensure that this kind of query still works
451
+ var result = db . Products
452
+ . GroupBy ( x => x . Supplier . CompanyName )
453
+ . Where ( x => db . Products . Count ( y => y . Supplier . CompanyName == x . Key && y . UnitPrice == 12.75M ) == 1 )
454
+ . Select ( x => new { x . Key , Count = x . Count ( ) } )
455
+ . First ( ) ;
456
+
457
+ Assert . That ( result . Key , Is . EqualTo ( "Zaanse Snoepfabriek" ) ) ;
458
+ Assert . That ( result . Count , Is . EqualTo ( 2 ) ) ;
459
+ }
460
+
461
+ [ Test ]
462
+ public void FilteredByAndProjectingSumWithPredicate ( )
463
+ {
464
+ var result = db . Products
465
+ . GroupBy ( x => x . Supplier . CompanyName )
466
+ . Where ( x => x . Sum ( y => y . UnitPrice == 12.75M ? y . UnitPrice : 0M ) == 12.75M )
467
+ . Select ( x => new { x . Key , Sum = x . Sum ( y => y . UnitPrice ) } )
468
+ . First ( ) ;
469
+
470
+ Assert . That ( result . Key , Is . EqualTo ( "Zaanse Snoepfabriek" ) ) ;
471
+ Assert . That ( result . Sum , Is . EqualTo ( 12.75M + 9.50M ) ) ;
472
+ }
473
+
474
+ [ Test ]
475
+ public void FilteredByKeyAndProjectedWithAggregatePredicates ( )
476
+ {
477
+ var result = db . Products
478
+ . GroupBy ( x => x . Supplier . CompanyName )
479
+ . Where ( x => x . Key == "Zaanse Snoepfabriek" )
480
+ . Select ( x => new { x . Key ,
481
+ Sum = x . Sum ( y => y . UnitPrice == 12.75M ? y . UnitPrice : 0M ) ,
482
+ Avg = x . Average ( y => y . UnitPrice == 12.75M ? y . UnitPrice : 0M ) ,
483
+ Count = x . Count ( y => y . UnitPrice == 12.75M ) ,
484
+ Max = x . Max ( y => y . UnitPrice == 12.75M ? y . UnitPrice : 0M ) ,
485
+ Min = x . Min ( y => y . UnitPrice == 12.75M ? y . UnitPrice : 0M )
486
+ } )
487
+ . First ( ) ;
488
+
489
+ Assert . That ( result . Key , Is . EqualTo ( "Zaanse Snoepfabriek" ) ) ;
490
+ Assert . That ( result . Sum , Is . EqualTo ( 12.75M ) ) ;
491
+ Assert . That ( result . Count , Is . EqualTo ( 1 ) ) ;
492
+ Assert . That ( result . Avg , Is . EqualTo ( 12.75M / 2 ) ) ;
493
+ Assert . That ( result . Max , Is . EqualTo ( 12.75M ) ) ;
494
+ Assert . That ( result . Min , Is . EqualTo ( 0M ) ) ;
495
+ }
496
+
497
+ [ Test ]
498
+ public void ProjectingWithSubQueriesFilteredByTheAggregateKey ( )
499
+ {
500
+ var result = db . Products . GroupBy ( x => x . Supplier . Address . Country )
501
+ . OrderBy ( x=> x . Key )
502
+ . Select ( x => new { x . Key , MaxFreight = db . Orders . Where ( y => y . ShippingAddress . Country == x . Key ) . Max ( y => y . Freight ) , FirstOrder = db . Orders . Where ( o => o . Employee . FirstName . StartsWith ( "A" ) ) . OrderBy ( o => o . OrderId ) . Select ( y => y . OrderId ) . First ( ) } )
503
+ . ToList ( ) ;
504
+
505
+ Assert . That ( result . Count , Is . EqualTo ( 16 ) ) ;
506
+ Assert . That ( result [ 15 ] . MaxFreight , Is . EqualTo ( 830.75M ) ) ;
507
+ Assert . That ( result [ 15 ] . FirstOrder , Is . EqualTo ( 10255 ) ) ;
508
+ }
509
+
421
510
private static void CheckGrouping < TKey , TElement > ( IEnumerable < IGrouping < TKey , TElement > > groupedItems , Func < TElement , TKey > groupBy )
422
511
{
423
512
var used = new HashSet < object > ( ) ;
0 commit comments