16
16
using System ;
17
17
using System . Collections . Generic ;
18
18
using System . Linq ;
19
- using System . Linq . Expressions ;
20
19
using System . Threading . Tasks ;
21
20
using FluentAssertions ;
22
21
using MongoDB . Bson ;
23
- using MongoDB . Bson . TestHelpers . XunitExtensions ;
24
22
using MongoDB . Driver ;
25
- using MongoDB . Driver . Core ;
26
23
using MongoDB . Driver . Core . TestHelpers . XunitExtensions ;
27
24
using MongoDB . Driver . Linq ;
28
25
using MongoDB . Driver . Tests . Linq ;
@@ -1148,6 +1145,61 @@ public void SelectMany_with_only_resultSelector()
1148
1145
"{ $project: { G: '$G', _id: 0 } }" ) ;
1149
1146
}
1150
1147
1148
+ [ Fact ]
1149
+ public void SelectMany_with_result_selector_which_has_subobjects ( )
1150
+ {
1151
+ var query = CreateQuery ( )
1152
+ . SelectMany ( x => x . C . X ) ;
1153
+
1154
+ Assert ( query ,
1155
+ 2 ,
1156
+ "{ $unwind : '$C.X' }" ,
1157
+ "{ $project : { X : '$C.X', _id : 0 } }" ) ;
1158
+ }
1159
+
1160
+ [ Fact ]
1161
+ public void SelectMany_with_result_selector_which_is_called_from_SelectMany ( )
1162
+ {
1163
+ var cQuery = CreateQuery ( )
1164
+ . SelectMany ( g => g . G )
1165
+ . SelectMany ( s => s . S ) ;
1166
+
1167
+ Assert ( cQuery ,
1168
+ 1 ,
1169
+ "{ $unwind : '$G' }" ,
1170
+ "{ $project : { G : '$G', _id : 0 } }" ,
1171
+ "{ $unwind : '$G.S' }" ,
1172
+ "{ $project : { S : '$G.S', _id : 0 } }" ) ;
1173
+
1174
+ var xQuery = CreateQuery ( )
1175
+ . SelectMany ( g => g . G )
1176
+ . SelectMany ( s => s . S )
1177
+ . SelectMany ( x => x . X ) ;
1178
+
1179
+ Assert ( xQuery ,
1180
+ 0 ,
1181
+ "{ $unwind : '$G' }" ,
1182
+ "{ $project : { G : '$G', _id : 0 } }" ,
1183
+ "{ $unwind : '$G.S' }" ,
1184
+ "{ $project : { S : '$G.S', _id : 0 } }" ,
1185
+ "{ $unwind : '$S.X' }" ,
1186
+ "{ $project : { X : '$S.X', _id : 0 } }" ) ;
1187
+ }
1188
+
1189
+ [ Fact ]
1190
+ public void SelectMany_with_result_selector_which_called_from_where ( )
1191
+ {
1192
+ var query = CreateQuery ( )
1193
+ . Where ( c => c . K )
1194
+ . SelectMany ( x => x . G ) ;
1195
+
1196
+ Assert ( query ,
1197
+ 2 ,
1198
+ "{ $match : { 'K' : true } }" ,
1199
+ "{ $unwind : '$G' }" ,
1200
+ "{ $project : { G : '$G', _id : 0 } }" ) ;
1201
+ }
1202
+
1151
1203
[ Fact ]
1152
1204
public void SelectMany_with_collection_selector_method_simple_scalar ( )
1153
1205
{
@@ -1160,6 +1212,57 @@ public void SelectMany_with_collection_selector_method_simple_scalar()
1160
1212
"{ $project: { G: '$G', _id: 0 } }" ) ;
1161
1213
}
1162
1214
1215
+ [ Fact ]
1216
+ public void SelectMany_with_collection_selector_method_simple_scalar_which_is_called_from_SelectMany ( )
1217
+ {
1218
+ var cQuery = CreateQuery ( )
1219
+ . SelectMany ( g => g . G , ( x , c ) => c )
1220
+ . SelectMany ( s => s . S ) ;
1221
+
1222
+ Assert ( cQuery ,
1223
+ 1 ,
1224
+ "{ $unwind : '$G' }" ,
1225
+ "{ $project : { G : '$G', _id : 0 } }" ,
1226
+ "{ $unwind : '$G.S' }" ,
1227
+ "{ $project : { S : '$G.S', _id : 0 } }" ) ;
1228
+
1229
+ cQuery = CreateQuery ( )
1230
+ . SelectMany ( g => g . G )
1231
+ . SelectMany ( s => s . S , ( x , c ) => c ) ;
1232
+
1233
+ Assert ( cQuery ,
1234
+ 1 ,
1235
+ "{ $unwind : '$G' }" ,
1236
+ "{ $project : { G : '$G', _id : 0 } }" ,
1237
+ "{ $unwind : '$G.S' }" ,
1238
+ "{ $project : { S : '$G.S', _id : 0 } }" ) ;
1239
+
1240
+ cQuery = CreateQuery ( )
1241
+ . SelectMany ( g => g . G , ( x , c ) => c )
1242
+ . SelectMany ( s => s . S , ( x , c ) => c ) ;
1243
+
1244
+ Assert ( cQuery ,
1245
+ 1 ,
1246
+ "{ $unwind : '$G' }" ,
1247
+ "{ $project : { G : '$G', _id : 0 } }" ,
1248
+ "{ $unwind : '$G.S' }" ,
1249
+ "{ $project : { S : '$G.S', _id : 0 } }" ) ;
1250
+
1251
+ var xQuery = CreateQuery ( )
1252
+ . SelectMany ( g => g . G , ( x , c ) => c )
1253
+ . SelectMany ( s => s . S , ( x , c ) => c )
1254
+ . SelectMany ( x => x . X , ( x , c ) => c ) ;
1255
+
1256
+ Assert ( xQuery ,
1257
+ 0 ,
1258
+ "{ $unwind : '$G' }" ,
1259
+ "{ $project : { G : '$G', _id : 0 } }" ,
1260
+ "{ $unwind : '$G.S' }" ,
1261
+ "{ $project : { S : '$G.S', _id : 0 } }" ,
1262
+ "{ $unwind : '$S.X' }" ,
1263
+ "{ $project : { X : '$S.X', _id : 0 } }" ) ;
1264
+ }
1265
+
1163
1266
[ Fact ]
1164
1267
public void SelectMany_with_collection_selector_syntax_simple_scalar ( )
1165
1268
{
@@ -1173,6 +1276,24 @@ from y in x.G
1173
1276
"{ $project: { G: '$G', _id: 0 } }" ) ;
1174
1277
}
1175
1278
1279
+ [ Fact ]
1280
+ public void SelectMany_with_collection_selector_syntax_simple_scalar_which_is_called_from_SelectMany ( )
1281
+ {
1282
+ var selectMany1 = from x in CreateQuery ( )
1283
+ from g in x . G
1284
+ select g ;
1285
+ var selectMany2 = from g in selectMany1
1286
+ from s in g . S
1287
+ select s ;
1288
+
1289
+ Assert ( selectMany2 ,
1290
+ 1 ,
1291
+ "{ $unwind : '$G' }" ,
1292
+ "{ $project : { G : '$G', _id : 0 } }" ,
1293
+ "{ $unwind : '$G.S' }" ,
1294
+ "{ $project : { S : '$G.S', _id : 0 } }" ) ;
1295
+ }
1296
+
1176
1297
[ Fact ]
1177
1298
public void SelectMany_with_collection_selector_method_computed_scalar ( )
1178
1299
{
@@ -1185,6 +1306,21 @@ public void SelectMany_with_collection_selector_method_computed_scalar()
1185
1306
"{ $project: { __fld0: { $add: ['$C.E.F', '$G.E.F', '$G.E.H'] }, _id: 0 } }" ) ;
1186
1307
}
1187
1308
1309
+ [ Fact ]
1310
+ public void SelectMany_with_collection_selector_method_computed_scalar_which_is_called_from_SelectMany ( )
1311
+ {
1312
+ var query = CreateQuery ( )
1313
+ . SelectMany ( g => g . G )
1314
+ . SelectMany ( s => s . S , ( x , c ) => ( int ? ) ( x . E . F + c . E . F + c . E . H ) ) ;
1315
+
1316
+ Assert ( query ,
1317
+ 1 ,
1318
+ "{ $unwind : '$G' }" ,
1319
+ "{ $project : { G : '$G', _id : 0 } }" ,
1320
+ "{ $unwind : '$G.S' }" ,
1321
+ "{ $project : { __fld0 : { $add : ['$G.E.F', '$G.S.E.F', '$G.S.E.H'] }, _id : 0 } }" ) ;
1322
+ }
1323
+
1188
1324
[ Fact ]
1189
1325
public void SelectMany_with_collection_selector_syntax_computed_scalar ( )
1190
1326
{
@@ -1198,6 +1334,24 @@ from y in x.G
1198
1334
"{ $project: { __fld0: { $add: ['$C.E.F', '$G.E.F', '$G.E.H'] }, _id: 0 } }" ) ;
1199
1335
}
1200
1336
1337
+ [ Fact ]
1338
+ public void SelectMany_with_collection_selector_syntax_computed_scalar_which_is_called_from_SelectMany ( )
1339
+ {
1340
+ var selectMany1 = from x in CreateQuery ( )
1341
+ from g in x . G
1342
+ select g ;
1343
+ var selectMany2 = from g in selectMany1
1344
+ from s in g . S
1345
+ select ( int ? ) ( g . E . F + s . E . F + s . E . H ) ;
1346
+
1347
+ Assert ( selectMany2 ,
1348
+ 1 ,
1349
+ "{ $unwind : '$G' }" ,
1350
+ "{ $project : { G : '$G', _id : 0 } }" ,
1351
+ "{ $unwind : '$G.S' }" ,
1352
+ "{ $project: { __fld0 : { $add : ['$G.E.F', '$G.S.E.F', '$G.S.E.H'] }, _id : 0 } }" ) ;
1353
+ }
1354
+
1201
1355
[ Fact ]
1202
1356
public void SelectMany_with_collection_selector_method_anonymous_type ( )
1203
1357
{
@@ -1210,6 +1364,21 @@ public void SelectMany_with_collection_selector_method_anonymous_type()
1210
1364
"{ $project: { F: '$C.E.F', Other: '$G.D', _id: 0 } }" ) ;
1211
1365
}
1212
1366
1367
+ [ Fact ]
1368
+ public void SelectMany_with_collection_selector_method_anonymous_type_which_is_called_from_SelectMany ( )
1369
+ {
1370
+ var query = CreateQuery ( )
1371
+ . SelectMany ( g => g . G )
1372
+ . SelectMany ( s => s . S , ( x , c ) => new { x . E . F , Other = c . D } ) ;
1373
+
1374
+ Assert ( query ,
1375
+ 1 ,
1376
+ "{ $unwind : '$G' }" ,
1377
+ "{ $project : { G : '$G', _id : 0 } }" ,
1378
+ "{ $unwind : '$G.S' }" ,
1379
+ "{ $project : { F : '$G.E.F', Other : '$G.S.D', _id : 0 } }" ) ;
1380
+ }
1381
+
1213
1382
[ Fact ]
1214
1383
public void SelectMany_with_collection_selector_syntax_anonymous_type ( )
1215
1384
{
@@ -1223,6 +1392,24 @@ from y in x.G
1223
1392
"{ $project: { F: '$C.E.F', Other: '$G.D', _id: 0 } }" ) ;
1224
1393
}
1225
1394
1395
+ [ Fact ]
1396
+ public void SelectMany_with_collection_selector_syntax_anonymous_type_which_is_called_from_SelectMany ( )
1397
+ {
1398
+ var selectMany1 = from x in CreateQuery ( )
1399
+ from g in x . G
1400
+ select g ;
1401
+ var selectMany2 = from g in selectMany1
1402
+ from s in g . S
1403
+ select new { g . E . F , Other = s . D } ;
1404
+
1405
+ Assert ( selectMany2 ,
1406
+ 1 ,
1407
+ "{ $unwind : '$G' }" ,
1408
+ "{ $project : { G : '$G', _id : 0 } }" ,
1409
+ "{ $unwind : '$G.S' }" ,
1410
+ "{ $project : { F : '$G.E.F', Other : '$G.S.D', _id : 0 } }" ) ;
1411
+ }
1412
+
1226
1413
[ Fact ]
1227
1414
public void SelectMany_followed_by_a_group ( )
1228
1415
{
@@ -1246,6 +1433,33 @@ group f by f.D into g
1246
1433
"{ $project: { Key: '$_id', SumF: '$__agg0', _id: 0 } }" ) ;
1247
1434
}
1248
1435
1436
+ [ Fact ]
1437
+ public void SelectMany_followed_by_a_group_which_is_called_from_SelectMany ( )
1438
+ {
1439
+ var selectMany1 = from x in CreateQuery ( )
1440
+ from g in x . G
1441
+ select g ;
1442
+ var selectMany2 = from g in selectMany1
1443
+ from s in g . S
1444
+ select s ;
1445
+ var query = from s in selectMany2
1446
+ group s by s . D into g
1447
+ select new
1448
+ {
1449
+ g . Key ,
1450
+ SumF = g . Sum ( x => x . E . F )
1451
+ } ;
1452
+
1453
+ Assert ( query ,
1454
+ 1 ,
1455
+ "{ $unwind : '$G' }" ,
1456
+ "{ $project : { G: '$G', _id : 0 } }" ,
1457
+ "{ $unwind : '$G.S' }" ,
1458
+ "{ $project : { 'S' : '$G.S', '_id' : 0 } }" ,
1459
+ "{ $group : { _id : '$S.D', __agg0 : { $sum : '$S.E.F' } } }" ,
1460
+ "{ $project : { Key : '$_id', SumF : '$__agg0', _id : 0 } }" ) ;
1461
+ }
1462
+
1249
1463
[ Fact ]
1250
1464
public void Single ( )
1251
1465
{
0 commit comments