@@ -81,6 +81,17 @@ public class D
81
81
{
82
82
[ BsonElement ( "z" ) ]
83
83
public int Z ; // use field instead of property to test fields also
84
+
85
+ public override bool Equals ( object obj )
86
+ {
87
+ if ( obj == null || GetType ( ) != obj . GetType ( ) ) { return false ; }
88
+ return Z == ( ( D ) obj ) . Z ;
89
+ }
90
+
91
+ public override int GetHashCode ( )
92
+ {
93
+ return Z . GetHashCode ( ) ;
94
+ }
84
95
}
85
96
86
97
// used to test some query operators that have an IEqualityComparer parameter
@@ -115,6 +126,11 @@ public int GetHashCode(int obj)
115
126
private MongoDatabase _database ;
116
127
private MongoCollection < C > _collection ;
117
128
private MongoCollection < SystemProfileInfo > _systemProfileCollection ;
129
+ private ObjectId id1 = ObjectId . GenerateNewId ( ) ;
130
+ private ObjectId id2 = ObjectId . GenerateNewId ( ) ;
131
+ private ObjectId id3 = ObjectId . GenerateNewId ( ) ;
132
+ private ObjectId id4 = ObjectId . GenerateNewId ( ) ;
133
+ private ObjectId id5 = ObjectId . GenerateNewId ( ) ;
118
134
119
135
[ TestFixtureSetUp ]
120
136
public void Setup ( )
@@ -127,11 +143,11 @@ public void Setup()
127
143
128
144
// documents inserted deliberately out of order to test sorting
129
145
_collection . Drop ( ) ;
130
- _collection . Insert ( new C { X = 2 , Y = 11 , D = new D { Z = 22 } , A = new [ ] { 2 , 3 , 4 } , L = new List < int > { 2 , 3 , 4 } } ) ;
131
- _collection . Insert ( new C { X = 1 , Y = 11 , D = new D { Z = 11 } , S = "x is 1" , SA = new string [ ] { "Tom" , "Dick" , "Harry" } } ) ;
132
- _collection . Insert ( new C { X = 3 , Y = 33 , D = new D { Z = 33 } , B = true , BA = new bool [ ] { true } , E = E . A , EA = new E [ ] { E . A , E . B } } ) ;
133
- _collection . Insert ( new C { X = 5 , Y = 44 , D = new D { Z = 55 } , DBRef = new MongoDBRef ( "db" , "c" , 1 ) } ) ;
134
- _collection . Insert ( new C { X = 4 , Y = 44 , D = new D { Z = 44 } } ) ;
146
+ _collection . Insert ( new C { Id = id2 , X = 2 , Y = 11 , D = new D { Z = 22 } , A = new [ ] { 2 , 3 , 4 } , L = new List < int > { 2 , 3 , 4 } } ) ;
147
+ _collection . Insert ( new C { Id = id1 , X = 1 , Y = 11 , D = new D { Z = 11 } , S = "x is 1" , SA = new string [ ] { "Tom" , "Dick" , "Harry" } } ) ;
148
+ _collection . Insert ( new C { Id = id3 , X = 3 , Y = 33 , D = new D { Z = 33 } , B = true , BA = new bool [ ] { true } , E = E . A , EA = new E [ ] { E . A , E . B } } ) ;
149
+ _collection . Insert ( new C { Id = id5 , X = 5 , Y = 44 , D = new D { Z = 55 } , DBRef = new MongoDBRef ( "db" , "c" , 1 ) } ) ;
150
+ _collection . Insert ( new C { Id = id4 , X = 4 , Y = 44 , D = new D { Z = 44 } } ) ;
135
151
}
136
152
137
153
[ Test ]
@@ -359,15 +375,116 @@ public void TestDefaultIfEmptyWithDefaultValue()
359
375
}
360
376
361
377
[ Test ]
362
- [ ExpectedException ( typeof ( InvalidOperationException ) , ExpectedMessage = "The Distinct query operator is not supported." ) ]
363
- public void TestDistinct ( )
378
+ public void TestDistinctB ( )
364
379
{
365
- var query = _collection . AsQueryable < C > ( ) . Distinct ( ) ;
366
- query . ToList ( ) ; // execute query
380
+ var query = ( from c in _collection . AsQueryable < C > ( )
381
+ select c . B ) . Distinct ( ) ;
382
+ var results = query . ToList ( ) ;
383
+ Assert . AreEqual ( 2 , results . Count ) ;
384
+ Assert . IsTrue ( results . Contains ( false ) ) ;
385
+ Assert . IsTrue ( results . Contains ( true ) ) ;
386
+ }
387
+
388
+ [ Test ]
389
+ public void TestDistinctD ( )
390
+ {
391
+ var query = ( from c in _collection . AsQueryable < C > ( )
392
+ select c . D ) . Distinct ( ) ;
393
+ var results = query . ToList ( ) ; // execute query
394
+ Assert . AreEqual ( 5 , results . Count ) ;
395
+ Assert . IsTrue ( results . Contains ( new D { Z = 11 } ) ) ;
396
+ Assert . IsTrue ( results . Contains ( new D { Z = 22 } ) ) ;
397
+ Assert . IsTrue ( results . Contains ( new D { Z = 33 } ) ) ;
398
+ Assert . IsTrue ( results . Contains ( new D { Z = 44 } ) ) ;
399
+ Assert . IsTrue ( results . Contains ( new D { Z = 55 } ) ) ;
400
+ }
401
+
402
+ [ Test ]
403
+ public void TestDistinctDBRef ( )
404
+ {
405
+ var query = ( from c in _collection . AsQueryable < C > ( )
406
+ select c . DBRef ) . Distinct ( ) ;
407
+ var results = query . ToList ( ) ;
408
+ Assert . AreEqual ( 1 , results . Count ) ;
409
+ Assert . IsTrue ( results . Contains ( new MongoDBRef ( "db" , "c" , 1 ) ) ) ;
410
+ }
411
+
412
+ [ Test ]
413
+ public void TestDistinctDZ ( )
414
+ {
415
+ var query = ( from c in _collection . AsQueryable < C > ( )
416
+ select c . D . Z ) . Distinct ( ) ;
417
+ var results = query . ToList ( ) ;
418
+ Assert . AreEqual ( 5 , results . Count ) ;
419
+ Assert . IsTrue ( results . Contains ( 11 ) ) ;
420
+ Assert . IsTrue ( results . Contains ( 22 ) ) ;
421
+ Assert . IsTrue ( results . Contains ( 33 ) ) ;
422
+ Assert . IsTrue ( results . Contains ( 44 ) ) ;
423
+ Assert . IsTrue ( results . Contains ( 55 ) ) ;
424
+ }
425
+
426
+ [ Test ]
427
+ public void TestDistinctE ( )
428
+ {
429
+ var query = ( from c in _collection . AsQueryable < C > ( )
430
+ select c . E ) . Distinct ( ) ;
431
+ var results = query . ToList ( ) ;
432
+ Assert . AreEqual ( 1 , results . Count ) ;
433
+ Assert . IsTrue ( results . Contains ( E . A ) ) ;
434
+ }
435
+
436
+ [ Test ]
437
+ public void TestDistinctId ( )
438
+ {
439
+ var query = ( from c in _collection . AsQueryable < C > ( )
440
+ select c . Id ) . Distinct ( ) ;
441
+ var results = query . ToList ( ) ;
442
+ Assert . AreEqual ( 5 , results . Count ) ;
443
+ Assert . IsTrue ( results . Contains ( id1 ) ) ;
444
+ Assert . IsTrue ( results . Contains ( id2 ) ) ;
445
+ Assert . IsTrue ( results . Contains ( id3 ) ) ;
446
+ Assert . IsTrue ( results . Contains ( id4 ) ) ;
447
+ Assert . IsTrue ( results . Contains ( id5 ) ) ;
448
+ }
449
+
450
+ [ Test ]
451
+ public void TestDistinctS ( )
452
+ {
453
+ var query = ( from c in _collection . AsQueryable < C > ( )
454
+ select c . S ) . Distinct ( ) ;
455
+ var results = query . ToList ( ) ;
456
+ Assert . AreEqual ( 1 , results . Count ) ;
457
+ Assert . IsTrue ( results . Contains ( "x is 1" ) ) ;
367
458
}
368
459
369
460
[ Test ]
370
- [ ExpectedException ( typeof ( InvalidOperationException ) , ExpectedMessage = "The Distinct query operator is not supported." ) ]
461
+ public void TestDistinctX ( )
462
+ {
463
+ var query = ( from c in _collection . AsQueryable < C > ( )
464
+ select c . X ) . Distinct ( ) ;
465
+ var results = query . ToList ( ) ;
466
+ Assert . AreEqual ( 5 , results . Count ) ;
467
+ Assert . IsTrue ( results . Contains ( 1 ) ) ;
468
+ Assert . IsTrue ( results . Contains ( 2 ) ) ;
469
+ Assert . IsTrue ( results . Contains ( 3 ) ) ;
470
+ Assert . IsTrue ( results . Contains ( 4 ) ) ;
471
+ Assert . IsTrue ( results . Contains ( 5 ) ) ;
472
+ }
473
+
474
+ [ Test ]
475
+ public void TestDistinctY ( )
476
+ {
477
+ var query = ( from c in _collection . AsQueryable < C > ( )
478
+ select c . Y ) . Distinct ( ) ;
479
+ var results = query . ToList ( ) ;
480
+ Assert . AreEqual ( 3 , results . Count ) ;
481
+ Assert . IsTrue ( results . Contains ( 11 ) ) ;
482
+ Assert . IsTrue ( results . Contains ( 33 ) ) ;
483
+ Assert . IsTrue ( results . Contains ( 44 ) ) ;
484
+ }
485
+
486
+ [ Test ]
487
+ [ ExpectedException ( typeof ( InvalidOperationException ) , ExpectedMessage = "The version of the Distinct query operator with an equality comparer is not supported." ) ]
371
488
public void TestDistinctWithEqualityComparer ( )
372
489
{
373
490
var query = _collection . AsQueryable < C > ( ) . Distinct ( new CEqualityComparer ( ) ) ;
@@ -1237,10 +1354,10 @@ public void TestProjection()
1237
1354
1238
1355
Assert . IsNull ( selectQuery . BuildQuery ( ) ) ;
1239
1356
1240
- var result = query . ToList ( ) ;
1241
- Assert . AreEqual ( 5 , result . Count ) ;
1242
- Assert . AreEqual ( 2 , result . First ( ) ) ;
1243
- Assert . AreEqual ( 4 , result . Last ( ) ) ;
1357
+ var results = query . ToList ( ) ;
1358
+ Assert . AreEqual ( 5 , results . Count ) ;
1359
+ Assert . AreEqual ( 2 , results . First ( ) ) ;
1360
+ Assert . AreEqual ( 4 , results . Last ( ) ) ;
1244
1361
}
1245
1362
1246
1363
[ Test ]
0 commit comments