-
Notifications
You must be signed in to change notification settings - Fork 3.4k
Expand file tree
/
Copy pathQueryableMethodTranslatingExpressionVisitor.cs
More file actions
1162 lines (1006 loc) · 65.1 KB
/
QueryableMethodTranslatingExpressionVisitor.cs
File metadata and controls
1162 lines (1006 loc) · 65.1 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
// Licensed to the .NET Foundation under one or more agreements.
// The .NET Foundation licenses this file to you under the MIT license.
using static Microsoft.EntityFrameworkCore.Infrastructure.ExpressionExtensions;
namespace Microsoft.EntityFrameworkCore.Query;
/// <summary>
/// <para>
/// A class that translates queryable methods in a query.
/// </para>
/// <para>
/// This type is typically used by database providers (and other extensions). It is generally
/// not used in application code.
/// </para>
/// </summary>
/// <param name="dependencies">Parameter object containing dependencies for this class.</param>
/// <param name="queryCompilationContext">The query compilation context object to use.</param>
/// <param name="subquery">A bool value indicating whether it is for a subquery translation.</param>
/// <remarks>
/// See <see href="https://aka.ms/efcore-docs-providers">Implementation of database providers and extensions</see>
/// and <see href="https://aka.ms/efcore-docs-how-query-works">How EF Core queries work</see> for more information and examples.
/// </remarks>
public abstract class QueryableMethodTranslatingExpressionVisitor(
QueryableMethodTranslatingExpressionVisitorDependencies dependencies,
QueryCompilationContext queryCompilationContext,
bool subquery)
: ExpressionVisitor
{
private readonly bool _subquery = subquery;
private readonly EntityShaperNullableMarkingExpressionVisitor _entityShaperNullableMarkingExpressionVisitor = new();
/// <summary>
/// Dependencies for this service.
/// </summary>
protected virtual QueryableMethodTranslatingExpressionVisitorDependencies Dependencies { get; } = dependencies;
private Expression? _untranslatedExpression;
/// <summary>
/// Detailed information about errors encountered during translation.
/// </summary>
public virtual string? TranslationErrorDetails { get; private set; }
/// <summary>
/// Translates an expression to an equivalent SQL representation.
/// </summary>
/// <param name="expression">An expression to translate.</param>
/// <returns>A SQL translation of the given expression.</returns>
public virtual Expression Translate(Expression expression)
{
var translated = Visit(expression);
// Note that we only throw if a specific node is recognized as untranslatable; we need to otherwise not throw in order to allow
// for client evaluation.
if (translated == QueryCompilationContext.NotTranslatedExpression && _untranslatedExpression is not null)
{
if (_untranslatedExpression is QueryRootExpression)
{
throw new InvalidOperationException(
TranslationErrorDetails is null
? CoreStrings.QueryUnhandledQueryRootExpression(_untranslatedExpression.GetType().ShortDisplayName())
: CoreStrings.TranslationFailedWithDetails(_untranslatedExpression, TranslationErrorDetails));
}
throw new InvalidOperationException(
TranslationErrorDetails is null
? CoreStrings.TranslationFailed(_untranslatedExpression.Print())
: CoreStrings.TranslationFailedWithDetails(_untranslatedExpression.Print(), TranslationErrorDetails));
}
return translated;
}
/// <summary>
/// Adds detailed information about errors encountered during translation.
/// </summary>
/// <param name="details">Error encountered during translation.</param>
protected virtual void AddTranslationErrorDetails(string details)
{
if (TranslationErrorDetails == null)
{
TranslationErrorDetails = details;
}
else
{
TranslationErrorDetails += Environment.NewLine + details;
}
}
/// <summary>
/// The query compilation context object for current compilation.
/// </summary>
protected virtual QueryCompilationContext QueryCompilationContext { get; } = queryCompilationContext;
/// <inheritdoc />
protected override Expression VisitExtension(Expression extensionExpression)
{
switch (extensionExpression)
{
case InlineQueryRootExpression inlineQueryRootExpression:
return TranslateInlineQueryRoot(inlineQueryRootExpression) ?? base.VisitExtension(extensionExpression);
case ParameterQueryRootExpression parameterQueryRootExpression:
return TranslateParameterQueryRoot(parameterQueryRootExpression) ?? base.VisitExtension(extensionExpression);
case QueryRootExpression queryRootExpression:
// This requires exact type match on query root to avoid processing query roots derived from EntityQueryRootExpression, e.g.
// SQL Server TemporalQueryRootExpression.
if (queryRootExpression.GetType() == typeof(EntityQueryRootExpression))
{
var shapedQuery = CreateShapedQueryExpression(((EntityQueryRootExpression)extensionExpression).EntityType);
if (shapedQuery is not null)
{
return shapedQuery;
}
}
_untranslatedExpression = queryRootExpression;
return QueryCompilationContext.NotTranslatedExpression;
default:
return base.VisitExtension(extensionExpression);
}
}
/// <inheritdoc />
protected override Expression VisitMethodCall(MethodCallExpression methodCallExpression)
{
var method = methodCallExpression.Method;
if (method.DeclaringType == typeof(EntityFrameworkQueryableExtensions))
{
var source = Visit(methodCallExpression.Arguments[0]);
if (source is ShapedQueryExpression shapedQueryExpression)
{
var genericMethod = method.IsGenericMethod ? method.GetGenericMethodDefinition() : null;
switch (method.Name)
{
case nameof(EntityFrameworkQueryableExtensions.ExecuteDelete)
when genericMethod == EntityFrameworkQueryableExtensions.ExecuteDeleteMethodInfo:
{
try
{
return TranslateExecuteDelete(shapedQueryExpression);
}
catch (Exception innerException)
{
throw new InvalidOperationException(
CoreStrings.NonQueryTranslationFailed(methodCallExpression.Print()),
innerException);
}
}
case nameof(EntityFrameworkQueryableExtensions.ExecuteUpdate)
when genericMethod == EntityFrameworkQueryableExtensions.ExecuteUpdateMethodInfo:
NewArrayExpression newArray;
{
newArray = methodCallExpression.Arguments[1] switch
{
NewArrayExpression n => n,
ConstantExpression { Value: Array { Length: 0 } }
=> throw new InvalidOperationException(
CoreStrings.NonQueryTranslationFailed(methodCallExpression.Print()),
new InvalidOperationException(CoreStrings.NoSetPropertyInvocation)),
_ => throw new UnreachableException("ExecuteUpdate with incorrect setters")
};
var setters = new ExecuteUpdateSetter[newArray.Expressions.Count];
for (var i = 0; i < setters.Length; i++)
{
var @new = (NewExpression)newArray.Expressions[i];
var propertySelector = (LambdaExpression)@new.Arguments[0];
var valueSelector = @new.Arguments[1];
// When the value selector is a bare value type (no lambda), a cast-to-object Convert node needs to be added
// for proper typing (see UpdateSettersBuilder); remove it here.
if (valueSelector is UnaryExpression { NodeType: ExpressionType.Convert, Operand: var unwrappedValueSelector }
&& valueSelector.Type == typeof(object))
{
valueSelector = unwrappedValueSelector;
}
setters[i] = new ExecuteUpdateSetter(propertySelector, valueSelector);
}
try
{
return TranslateExecuteUpdate(shapedQueryExpression, setters);
}
catch (Exception innerException)
{
throw new InvalidOperationException(
CoreStrings.NonQueryTranslationFailed(methodCallExpression.Print()),
innerException);
}
}
}
}
}
if (method.DeclaringType == typeof(Queryable))
{
var source = Visit(methodCallExpression.Arguments[0]);
if (source is ShapedQueryExpression shapedQueryExpression)
{
var genericMethod = method.IsGenericMethod ? method.GetGenericMethodDefinition() : null;
switch (method.Name)
{
case nameof(Queryable.All)
when genericMethod == QueryableMethods.All:
shapedQueryExpression = shapedQueryExpression.UpdateResultCardinality(ResultCardinality.Single);
return CheckTranslated(TranslateAll(shapedQueryExpression, GetLambdaExpressionFromArgument(1)));
case nameof(Queryable.Any)
when genericMethod == QueryableMethods.AnyWithoutPredicate:
shapedQueryExpression = shapedQueryExpression.UpdateResultCardinality(ResultCardinality.Single);
return CheckTranslated(TranslateAny(shapedQueryExpression, null));
case nameof(Queryable.Any)
when genericMethod == QueryableMethods.AnyWithPredicate:
shapedQueryExpression = shapedQueryExpression.UpdateResultCardinality(ResultCardinality.Single);
return CheckTranslated(TranslateAny(shapedQueryExpression, GetLambdaExpressionFromArgument(1)));
case nameof(Queryable.AsQueryable)
when genericMethod == QueryableMethods.AsQueryable:
return source;
case nameof(Queryable.Average)
when QueryableMethods.IsAverageWithoutSelector(method):
shapedQueryExpression = shapedQueryExpression.UpdateResultCardinality(ResultCardinality.Single);
return CheckTranslated(TranslateAverage(shapedQueryExpression, null, methodCallExpression.Type));
case nameof(Queryable.Average)
when QueryableMethods.IsAverageWithSelector(method):
shapedQueryExpression = shapedQueryExpression.UpdateResultCardinality(ResultCardinality.Single);
return CheckTranslated(
TranslateAverage(shapedQueryExpression, GetLambdaExpressionFromArgument(1), methodCallExpression.Type));
case nameof(Queryable.Cast)
when genericMethod == QueryableMethods.Cast:
return CheckTranslated(TranslateCast(shapedQueryExpression, method.GetGenericArguments()[0]));
case nameof(Queryable.Concat)
when genericMethod == QueryableMethods.Concat:
{
var source2 = Visit(methodCallExpression.Arguments[1]);
if (source2 is ShapedQueryExpression innerShapedQueryExpression)
{
return CheckTranslated(TranslateConcat(shapedQueryExpression, innerShapedQueryExpression));
}
break;
}
case nameof(Queryable.Contains)
when genericMethod == QueryableMethods.Contains:
shapedQueryExpression = shapedQueryExpression.UpdateResultCardinality(ResultCardinality.Single);
return CheckTranslated(TranslateContains(shapedQueryExpression, methodCallExpression.Arguments[1]));
case nameof(Queryable.Count)
when genericMethod == QueryableMethods.CountWithoutPredicate:
shapedQueryExpression = shapedQueryExpression.UpdateResultCardinality(ResultCardinality.Single);
return CheckTranslated(TranslateCount(shapedQueryExpression, null));
case nameof(Queryable.Count)
when genericMethod == QueryableMethods.CountWithPredicate:
shapedQueryExpression = shapedQueryExpression.UpdateResultCardinality(ResultCardinality.Single);
return CheckTranslated(TranslateCount(shapedQueryExpression, GetLambdaExpressionFromArgument(1)));
case nameof(Queryable.DefaultIfEmpty)
when genericMethod == QueryableMethods.DefaultIfEmptyWithoutArgument:
return CheckTranslated(TranslateDefaultIfEmpty(shapedQueryExpression, null));
case nameof(Queryable.DefaultIfEmpty)
when genericMethod == QueryableMethods.DefaultIfEmptyWithArgument:
return CheckTranslated(TranslateDefaultIfEmpty(shapedQueryExpression, methodCallExpression.Arguments[1]));
case nameof(Queryable.Distinct)
when genericMethod == QueryableMethods.Distinct:
return CheckTranslated(TranslateDistinct(shapedQueryExpression));
case nameof(Queryable.ElementAt)
when genericMethod == QueryableMethods.ElementAt:
shapedQueryExpression = shapedQueryExpression.UpdateResultCardinality(ResultCardinality.Single);
return CheckTranslated(
TranslateElementAtOrDefault(shapedQueryExpression, methodCallExpression.Arguments[1], false));
case nameof(Queryable.ElementAtOrDefault)
when genericMethod == QueryableMethods.ElementAtOrDefault:
shapedQueryExpression = shapedQueryExpression.UpdateResultCardinality(ResultCardinality.SingleOrDefault);
return CheckTranslated(
TranslateElementAtOrDefault(shapedQueryExpression, methodCallExpression.Arguments[1], true));
case nameof(Queryable.Except)
when genericMethod == QueryableMethods.Except:
{
var source2 = Visit(methodCallExpression.Arguments[1]);
if (source2 is ShapedQueryExpression innerShapedQueryExpression)
{
return CheckTranslated(TranslateExcept(shapedQueryExpression, innerShapedQueryExpression));
}
break;
}
case nameof(Queryable.First)
when genericMethod == QueryableMethods.FirstWithoutPredicate:
shapedQueryExpression = shapedQueryExpression.UpdateResultCardinality(ResultCardinality.Single);
return CheckTranslated(TranslateFirstOrDefault(shapedQueryExpression, null, methodCallExpression.Type, false));
case nameof(Queryable.First)
when genericMethod == QueryableMethods.FirstWithPredicate:
shapedQueryExpression = shapedQueryExpression.UpdateResultCardinality(ResultCardinality.Single);
return CheckTranslated(
TranslateFirstOrDefault(
shapedQueryExpression, GetLambdaExpressionFromArgument(1), methodCallExpression.Type, false));
case nameof(Queryable.FirstOrDefault)
when genericMethod == QueryableMethods.FirstOrDefaultWithoutPredicate:
shapedQueryExpression = shapedQueryExpression.UpdateResultCardinality(ResultCardinality.SingleOrDefault);
return CheckTranslated(TranslateFirstOrDefault(shapedQueryExpression, null, methodCallExpression.Type, true));
case nameof(Queryable.FirstOrDefault)
when genericMethod == QueryableMethods.FirstOrDefaultWithPredicate:
shapedQueryExpression = shapedQueryExpression.UpdateResultCardinality(ResultCardinality.SingleOrDefault);
return CheckTranslated(
TranslateFirstOrDefault(
shapedQueryExpression, GetLambdaExpressionFromArgument(1), methodCallExpression.Type, true));
case nameof(Queryable.GroupBy)
when genericMethod == QueryableMethods.GroupByWithKeySelector:
return CheckTranslated(TranslateGroupBy(shapedQueryExpression, GetLambdaExpressionFromArgument(1), null, null));
case nameof(Queryable.GroupBy)
when genericMethod == QueryableMethods.GroupByWithKeyElementSelector:
return CheckTranslated(
TranslateGroupBy(
shapedQueryExpression, GetLambdaExpressionFromArgument(1), GetLambdaExpressionFromArgument(2), null));
case nameof(Queryable.GroupBy)
when genericMethod == QueryableMethods.GroupByWithKeyElementResultSelector:
return CheckTranslated(
TranslateGroupBy(
shapedQueryExpression, GetLambdaExpressionFromArgument(1), GetLambdaExpressionFromArgument(2),
GetLambdaExpressionFromArgument(3)));
case nameof(Queryable.GroupBy)
when genericMethod == QueryableMethods.GroupByWithKeyResultSelector:
return CheckTranslated(
TranslateGroupBy(
shapedQueryExpression, GetLambdaExpressionFromArgument(1), null, GetLambdaExpressionFromArgument(2)));
case nameof(Queryable.GroupJoin)
when genericMethod == QueryableMethods.GroupJoin:
{
if (Visit(methodCallExpression.Arguments[1]) is ShapedQueryExpression innerShapedQueryExpression)
{
return CheckTranslated(
TranslateGroupJoin(
shapedQueryExpression,
innerShapedQueryExpression,
GetLambdaExpressionFromArgument(2),
GetLambdaExpressionFromArgument(3),
GetLambdaExpressionFromArgument(4)));
}
break;
}
case nameof(Queryable.Intersect)
when genericMethod == QueryableMethods.Intersect:
{
if (Visit(methodCallExpression.Arguments[1]) is ShapedQueryExpression innerShapedQueryExpression)
{
return CheckTranslated(TranslateIntersect(shapedQueryExpression, innerShapedQueryExpression));
}
break;
}
case nameof(Queryable.Join)
when genericMethod == QueryableMethods.Join:
{
if (Visit(methodCallExpression.Arguments[1]) is ShapedQueryExpression innerShapedQueryExpression)
{
return CheckTranslated(
TranslateJoin(
shapedQueryExpression, innerShapedQueryExpression, GetLambdaExpressionFromArgument(2),
GetLambdaExpressionFromArgument(3), GetLambdaExpressionFromArgument(4)));
}
break;
}
case nameof(Queryable.LeftJoin)
when genericMethod == QueryableMethods.LeftJoin:
{
if (Visit(methodCallExpression.Arguments[1]) is ShapedQueryExpression innerShapedQueryExpression)
{
return CheckTranslated(
TranslateLeftJoin(
shapedQueryExpression, innerShapedQueryExpression, GetLambdaExpressionFromArgument(2),
GetLambdaExpressionFromArgument(3), GetLambdaExpressionFromArgument(4)));
}
break;
}
case nameof(Queryable.RightJoin)
when genericMethod == QueryableMethods.RightJoin:
{
if (Visit(methodCallExpression.Arguments[1]) is ShapedQueryExpression innerShapedQueryExpression)
{
return CheckTranslated(
TranslateRightJoin(
shapedQueryExpression, innerShapedQueryExpression, GetLambdaExpressionFromArgument(2),
GetLambdaExpressionFromArgument(3), GetLambdaExpressionFromArgument(4)));
}
break;
}
case nameof(Queryable.Last)
when genericMethod == QueryableMethods.LastWithoutPredicate:
shapedQueryExpression = shapedQueryExpression.UpdateResultCardinality(ResultCardinality.Single);
return CheckTranslated(TranslateLastOrDefault(shapedQueryExpression, null, methodCallExpression.Type, false));
case nameof(Queryable.Last)
when genericMethod == QueryableMethods.LastWithPredicate:
shapedQueryExpression = shapedQueryExpression.UpdateResultCardinality(ResultCardinality.Single);
return CheckTranslated(
TranslateLastOrDefault(
shapedQueryExpression, GetLambdaExpressionFromArgument(1), methodCallExpression.Type, false));
case nameof(Queryable.LastOrDefault)
when genericMethod == QueryableMethods.LastOrDefaultWithoutPredicate:
shapedQueryExpression = shapedQueryExpression.UpdateResultCardinality(ResultCardinality.SingleOrDefault);
return CheckTranslated(TranslateLastOrDefault(shapedQueryExpression, null, methodCallExpression.Type, true));
case nameof(Queryable.LastOrDefault)
when genericMethod == QueryableMethods.LastOrDefaultWithPredicate:
shapedQueryExpression = shapedQueryExpression.UpdateResultCardinality(ResultCardinality.SingleOrDefault);
return CheckTranslated(
TranslateLastOrDefault(
shapedQueryExpression, GetLambdaExpressionFromArgument(1), methodCallExpression.Type, true));
case nameof(Queryable.LongCount)
when genericMethod == QueryableMethods.LongCountWithoutPredicate:
shapedQueryExpression = shapedQueryExpression.UpdateResultCardinality(ResultCardinality.Single);
return CheckTranslated(TranslateLongCount(shapedQueryExpression, null));
case nameof(Queryable.LongCount)
when genericMethod == QueryableMethods.LongCountWithPredicate:
shapedQueryExpression = shapedQueryExpression.UpdateResultCardinality(ResultCardinality.Single);
return CheckTranslated(TranslateLongCount(shapedQueryExpression, GetLambdaExpressionFromArgument(1)));
case nameof(Queryable.Max)
when genericMethod == QueryableMethods.MaxWithoutSelector:
shapedQueryExpression = shapedQueryExpression.UpdateResultCardinality(ResultCardinality.Single);
return CheckTranslated(TranslateMax(shapedQueryExpression, null, methodCallExpression.Type));
case nameof(Queryable.Max)
when genericMethod == QueryableMethods.MaxWithSelector:
shapedQueryExpression = shapedQueryExpression.UpdateResultCardinality(ResultCardinality.Single);
return CheckTranslated(
TranslateMax(shapedQueryExpression, GetLambdaExpressionFromArgument(1), methodCallExpression.Type));
case nameof(Queryable.Min)
when genericMethod == QueryableMethods.MinWithoutSelector:
shapedQueryExpression = shapedQueryExpression.UpdateResultCardinality(ResultCardinality.Single);
return CheckTranslated(TranslateMin(shapedQueryExpression, null, methodCallExpression.Type));
case nameof(Queryable.Min)
when genericMethod == QueryableMethods.MinWithSelector:
shapedQueryExpression = shapedQueryExpression.UpdateResultCardinality(ResultCardinality.Single);
return CheckTranslated(
TranslateMin(shapedQueryExpression, GetLambdaExpressionFromArgument(1), methodCallExpression.Type));
case nameof(Queryable.OfType)
when genericMethod == QueryableMethods.OfType:
return CheckTranslated(TranslateOfType(shapedQueryExpression, method.GetGenericArguments()[0]));
case nameof(Queryable.OrderBy)
when genericMethod == QueryableMethods.OrderBy:
return CheckTranslated(TranslateOrderBy(shapedQueryExpression, GetLambdaExpressionFromArgument(1), true));
case nameof(Queryable.OrderByDescending)
when genericMethod == QueryableMethods.OrderByDescending:
return CheckTranslated(TranslateOrderBy(shapedQueryExpression, GetLambdaExpressionFromArgument(1), false));
case nameof(Queryable.Reverse)
when genericMethod == QueryableMethods.Reverse:
return CheckTranslated(TranslateReverse(shapedQueryExpression));
case nameof(Queryable.Select)
when genericMethod == QueryableMethods.Select:
return CheckTranslated(TranslateSelect(shapedQueryExpression, GetLambdaExpressionFromArgument(1)));
case nameof(Queryable.SelectMany)
when genericMethod == QueryableMethods.SelectManyWithoutCollectionSelector:
return CheckTranslated(TranslateSelectMany(shapedQueryExpression, GetLambdaExpressionFromArgument(1)));
case nameof(Queryable.SelectMany)
when genericMethod == QueryableMethods.SelectManyWithCollectionSelector:
return CheckTranslated(
TranslateSelectMany(
shapedQueryExpression, GetLambdaExpressionFromArgument(1), GetLambdaExpressionFromArgument(2)));
case nameof(Queryable.Single)
when genericMethod == QueryableMethods.SingleWithoutPredicate:
shapedQueryExpression = shapedQueryExpression.UpdateResultCardinality(ResultCardinality.Single);
return CheckTranslated(TranslateSingleOrDefault(shapedQueryExpression, null, methodCallExpression.Type, false));
case nameof(Queryable.Single)
when genericMethod == QueryableMethods.SingleWithPredicate:
shapedQueryExpression = shapedQueryExpression.UpdateResultCardinality(ResultCardinality.Single);
return CheckTranslated(
TranslateSingleOrDefault(
shapedQueryExpression, GetLambdaExpressionFromArgument(1), methodCallExpression.Type, false));
case nameof(Queryable.SingleOrDefault)
when genericMethod == QueryableMethods.SingleOrDefaultWithoutPredicate:
shapedQueryExpression = shapedQueryExpression.UpdateResultCardinality(ResultCardinality.SingleOrDefault);
return CheckTranslated(TranslateSingleOrDefault(shapedQueryExpression, null, methodCallExpression.Type, true));
case nameof(Queryable.SingleOrDefault)
when genericMethod == QueryableMethods.SingleOrDefaultWithPredicate:
shapedQueryExpression = shapedQueryExpression.UpdateResultCardinality(ResultCardinality.SingleOrDefault);
return CheckTranslated(
TranslateSingleOrDefault(
shapedQueryExpression, GetLambdaExpressionFromArgument(1), methodCallExpression.Type, true));
case nameof(Queryable.Skip)
when genericMethod == QueryableMethods.Skip:
return CheckTranslated(TranslateSkip(shapedQueryExpression, methodCallExpression.Arguments[1]));
case nameof(Queryable.SkipWhile)
when genericMethod == QueryableMethods.SkipWhile:
return CheckTranslated(TranslateSkipWhile(shapedQueryExpression, GetLambdaExpressionFromArgument(1)));
case nameof(Queryable.Sum)
when QueryableMethods.IsSumWithoutSelector(method):
shapedQueryExpression = shapedQueryExpression.UpdateResultCardinality(ResultCardinality.Single);
return CheckTranslated(TranslateSum(shapedQueryExpression, null, methodCallExpression.Type));
case nameof(Queryable.Sum)
when QueryableMethods.IsSumWithSelector(method):
shapedQueryExpression = shapedQueryExpression.UpdateResultCardinality(ResultCardinality.Single);
return CheckTranslated(
TranslateSum(shapedQueryExpression, GetLambdaExpressionFromArgument(1), methodCallExpression.Type));
case nameof(Queryable.Take)
when genericMethod == QueryableMethods.Take:
return CheckTranslated(TranslateTake(shapedQueryExpression, methodCallExpression.Arguments[1]));
case nameof(Queryable.TakeWhile)
when genericMethod == QueryableMethods.TakeWhile:
return CheckTranslated(TranslateTakeWhile(shapedQueryExpression, GetLambdaExpressionFromArgument(1)));
case nameof(Queryable.ThenBy)
when genericMethod == QueryableMethods.ThenBy:
return CheckTranslated(TranslateThenBy(shapedQueryExpression, GetLambdaExpressionFromArgument(1), true));
case nameof(Queryable.ThenByDescending)
when genericMethod == QueryableMethods.ThenByDescending:
return CheckTranslated(TranslateThenBy(shapedQueryExpression, GetLambdaExpressionFromArgument(1), false));
case nameof(Queryable.Union)
when genericMethod == QueryableMethods.Union:
{
if (Visit(methodCallExpression.Arguments[1]) is ShapedQueryExpression innerShapedQueryExpression)
{
return CheckTranslated(TranslateUnion(shapedQueryExpression, innerShapedQueryExpression));
}
break;
}
case nameof(Queryable.Where)
when genericMethod == QueryableMethods.Where:
return CheckTranslated(TranslateWhere(shapedQueryExpression, GetLambdaExpressionFromArgument(1)));
[DebuggerStepThrough]
LambdaExpression GetLambdaExpressionFromArgument(int argumentIndex)
=> methodCallExpression.Arguments[argumentIndex].UnwrapLambdaFromQuote();
[DebuggerStepThrough]
Expression CheckTranslated(ShapedQueryExpression? translated)
{
if (translated is not null)
{
return translated;
}
_untranslatedExpression ??= methodCallExpression;
return QueryCompilationContext.NotTranslatedExpression;
}
}
}
else if (source == QueryCompilationContext.NotTranslatedExpression)
{
return source;
}
}
// The method isn't a LINQ operator on Queryable/QueryableExtensions.
// Identify property access, i.e. primitive collection property (context.Blogs.Where(b => b.Tags.Contains(...))),
// complex collection property...
if (IsMemberAccess(methodCallExpression, QueryCompilationContext.Model, out var propertyAccessSource, out var propertyName)
&& TranslateMemberAccess(propertyAccessSource, propertyName) is { } translation)
{
return translation;
}
return _subquery
? QueryCompilationContext.NotTranslatedExpression
: TranslationErrorDetails is null
? throw new InvalidOperationException(CoreStrings.TranslationFailed(methodCallExpression.Print()))
: throw new InvalidOperationException(
CoreStrings.TranslationFailedWithDetails(methodCallExpression.Print(), TranslationErrorDetails));
}
/// <inheritdoc />
protected override Expression VisitMember(MemberExpression memberExpression)
{
// Identify property access, i.e. primitive collection property (context.Blogs.Where(b => b.Tags.Contains(...))),
// complex collection property...
if (IsMemberAccess(memberExpression, QueryCompilationContext.Model, out var propertyAccessSource, out var propertyName)
&& TranslateMemberAccess(propertyAccessSource, propertyName) is { } translation)
{
return translation;
}
return _subquery
? QueryCompilationContext.NotTranslatedExpression
: TranslationErrorDetails is null
? throw new InvalidOperationException(CoreStrings.TranslationFailed(memberExpression.Print()))
: throw new InvalidOperationException(
CoreStrings.TranslationFailedWithDetails(memberExpression.Print(), TranslationErrorDetails));
}
private sealed class EntityShaperNullableMarkingExpressionVisitor : ExpressionVisitor
{
protected override Expression VisitExtension(Expression extensionExpression)
=> extensionExpression is StructuralTypeShaperExpression shaper
? shaper.MakeNullable()
: base.VisitExtension(extensionExpression);
}
/// <summary>
/// Marks the entity shaper in the given shaper expression as nullable.
/// </summary>
/// <param name="shaperExpression">The shaper expression to process.</param>
/// <returns>New shaper expression in which all entity shapers are nullable.</returns>
protected virtual Expression MarkShaperNullable(Expression shaperExpression)
=> _entityShaperNullableMarkingExpressionVisitor.Visit(shaperExpression);
/// <summary>
/// Translates the given subquery.
/// </summary>
/// <param name="expression">The subquery expression to translate.</param>
/// <returns>The translation of the given subquery.</returns>
public virtual ShapedQueryExpression? TranslateSubquery(Expression expression)
{
var subqueryVisitor = CreateSubqueryVisitor();
var translation = subqueryVisitor.Translate(expression) as ShapedQueryExpression;
if (translation == null && subqueryVisitor.TranslationErrorDetails != null)
{
AddTranslationErrorDetails(subqueryVisitor.TranslationErrorDetails);
}
return translation;
}
/// <summary>
/// Creates a visitor customized to translate a subquery through <see cref="TranslateSubquery(Expression)" />.
/// </summary>
/// <returns>A visitor to translate subquery.</returns>
protected abstract QueryableMethodTranslatingExpressionVisitor CreateSubqueryVisitor();
/// <summary>
/// Creates a <see cref="ShapedQueryExpression" /> for the given entity type.
/// </summary>
/// <param name="entityType">The entity type.</param>
/// <returns>A shaped query expression for the given entity type.</returns>
protected abstract ShapedQueryExpression? CreateShapedQueryExpression(IEntityType entityType);
/// <summary>
/// Translates <see cref="Queryable.All{TSource}(IQueryable{TSource}, Expression{Func{TSource,bool}})" /> method over the given source.
/// </summary>
/// <param name="source">The shaped query on which the operator is applied.</param>
/// <param name="predicate">The predicate supplied in the call.</param>
/// <returns>The shaped query after translation.</returns>
protected abstract ShapedQueryExpression? TranslateAll(ShapedQueryExpression source, LambdaExpression predicate);
/// <summary>
/// Translates <see cref="Queryable.Any{TSource}(IQueryable{TSource})" /> method and other overloads over the given source.
/// </summary>
/// <param name="source">The shaped query on which the operator is applied.</param>
/// <param name="predicate">The predicate supplied in the call.</param>
/// <returns>The shaped query after translation.</returns>
protected abstract ShapedQueryExpression? TranslateAny(ShapedQueryExpression source, LambdaExpression? predicate);
/// <summary>
/// Translates <see cref="Queryable.Average(IQueryable{decimal})" /> method and other overloads over the given source.
/// </summary>
/// <param name="source">The shaped query on which the operator is applied.</param>
/// <param name="selector">The selector supplied in the call.</param>
/// <param name="resultType">The result type after the operation.</param>
/// <returns>The shaped query after translation.</returns>
protected abstract ShapedQueryExpression? TranslateAverage(ShapedQueryExpression source, LambdaExpression? selector, Type resultType);
/// <summary>
/// Translates <see cref="Queryable.Cast{TResult}(IQueryable)" /> method over the given source.
/// </summary>
/// <param name="source">The shaped query on which the operator is applied.</param>
/// <param name="castType">The type result is being casted to.</param>
/// <returns>The shaped query after translation.</returns>
protected abstract ShapedQueryExpression? TranslateCast(ShapedQueryExpression source, Type castType);
/// <summary>
/// Translates <see cref="Queryable.Concat{TSource}(IQueryable{TSource}, IEnumerable{TSource})" /> method over the given source.
/// </summary>
/// <param name="source1">The shaped query on which the operator is applied.</param>
/// <param name="source2">The other source to perform concat.</param>
/// <returns>The shaped query after translation.</returns>
protected abstract ShapedQueryExpression? TranslateConcat(ShapedQueryExpression source1, ShapedQueryExpression source2);
/// <summary>
/// Translates <see cref="Queryable.Contains{TSource}(IQueryable{TSource}, TSource)" /> method over the given source.
/// </summary>
/// <param name="source">The shaped query on which the operator is applied.</param>
/// <param name="item">The item to search for.</param>
/// <returns>The shaped query after translation.</returns>
protected abstract ShapedQueryExpression? TranslateContains(ShapedQueryExpression source, Expression item);
/// <summary>
/// Translates <see cref="Queryable.Count{TSource}(IQueryable{TSource})" /> method and other overloads over the given source.
/// </summary>
/// <param name="source">The shaped query on which the operator is applied.</param>
/// <param name="predicate">The predicate supplied in the call.</param>
/// <returns>The shaped query after translation.</returns>
protected abstract ShapedQueryExpression? TranslateCount(ShapedQueryExpression source, LambdaExpression? predicate);
/// <summary>
/// Translates <see cref="Queryable.DefaultIfEmpty{TSource}(IQueryable{TSource})" /> method and other overloads over the given source.
/// </summary>
/// <param name="source">The shaped query on which the operator is applied.</param>
/// <param name="defaultValue">The default value to use.</param>
/// <returns>The shaped query after translation.</returns>
protected abstract ShapedQueryExpression? TranslateDefaultIfEmpty(ShapedQueryExpression source, Expression? defaultValue);
/// <summary>
/// Translates <see cref="Queryable.Distinct{TSource}(IQueryable{TSource})" /> method over the given source.
/// </summary>
/// <param name="source">The shaped query on which the operator is applied.</param>
/// <returns>The shaped query after translation.</returns>
protected abstract ShapedQueryExpression? TranslateDistinct(ShapedQueryExpression source);
/// <summary>
/// Translates <see cref="Queryable.ElementAt{TSource}(IQueryable{TSource}, int)" /> method or
/// <see cref="Queryable.ElementAtOrDefault{TSource}(IQueryable{TSource}, int)" /> over the given source.
/// </summary>
/// <param name="source">The shaped query on which the operator is applied.</param>
/// <param name="index">The index of the element.</param>
/// <param name="returnDefault">A value indicating whether default should be returned or throw.</param>
/// <returns>The shaped query after translation.</returns>
protected abstract ShapedQueryExpression? TranslateElementAtOrDefault(
ShapedQueryExpression source,
Expression index,
bool returnDefault);
/// <summary>
/// Translates <see cref="Queryable.Except{TSource}(IQueryable{TSource}, IEnumerable{TSource})" /> method over the given source.
/// </summary>
/// <param name="source1">The shaped query on which the operator is applied.</param>
/// <param name="source2">The other source to perform except with.</param>
/// <returns>The shaped query after translation.</returns>
protected abstract ShapedQueryExpression? TranslateExcept(ShapedQueryExpression source1, ShapedQueryExpression source2);
/// <summary>
/// Translates <see cref="Queryable.First{TSource}(IQueryable{TSource})" /> method or
/// <see cref="Queryable.FirstOrDefault{TSource}(IQueryable{TSource})" /> and their other overloads over the given source.
/// </summary>
/// <param name="source">The shaped query on which the operator is applied.</param>
/// <param name="predicate">The predicate supplied in the call.</param>
/// <param name="returnType">The return type of result.</param>
/// <param name="returnDefault">A value indicating whether default should be returned or throw.</param>
/// <returns>The shaped query after translation.</returns>
protected abstract ShapedQueryExpression? TranslateFirstOrDefault(
ShapedQueryExpression source,
LambdaExpression? predicate,
Type returnType,
bool returnDefault);
/// <summary>
/// Translates <see cref="Queryable.GroupBy{TSource, TKey}(IQueryable{TSource}, Expression{Func{TSource, TKey}})" /> method and
/// other overloads over the given source.
/// </summary>
/// <param name="source">The shaped query on which the operator is applied.</param>
/// <param name="keySelector">The key selector supplied in the call.</param>
/// <param name="elementSelector">The element selector supplied in the call.</param>
/// <param name="resultSelector">The result selector supplied in the call.</param>
/// <returns>The shaped query after translation.</returns>
protected abstract ShapedQueryExpression? TranslateGroupBy(
ShapedQueryExpression source,
LambdaExpression keySelector,
LambdaExpression? elementSelector,
LambdaExpression? resultSelector);
/// <summary>
/// Translates
/// <see
/// cref="Queryable.GroupJoin{TOuter, TInner, TKey, TResult}(IQueryable{TOuter}, IEnumerable{TInner}, Expression{Func{TOuter, TKey}}, Expression{Func{TInner, TKey}}, Expression{Func{TOuter, IEnumerable{TInner}, TResult}})" />
/// method over the given source.
/// </summary>
/// <param name="outer">The shaped query on which the operator is applied.</param>
/// <param name="inner">The inner shaped query to perform join with.</param>
/// <param name="outerKeySelector">The key selector for the outer source.</param>
/// <param name="innerKeySelector">The key selector for the inner source.</param>
/// <param name="resultSelector">The result selector supplied in the call.</param>
/// <returns>The shaped query after translation.</returns>
protected abstract ShapedQueryExpression? TranslateGroupJoin(
ShapedQueryExpression outer,
ShapedQueryExpression inner,
LambdaExpression outerKeySelector,
LambdaExpression innerKeySelector,
LambdaExpression resultSelector);
/// <summary>
/// Translates <see cref="Queryable.Intersect{TSource}(IQueryable{TSource}, IEnumerable{TSource})" /> method over the given source.
/// </summary>
/// <param name="source1">The shaped query on which the operator is applied.</param>
/// <param name="source2">The other source to perform intersect with.</param>
/// <returns>The shaped query after translation.</returns>
protected abstract ShapedQueryExpression? TranslateIntersect(ShapedQueryExpression source1, ShapedQueryExpression source2);
/// <summary>
/// Translates
/// <see
/// cref="Queryable.Join{TOuter, TInner, TKey, TResult}(IQueryable{TOuter}, IEnumerable{TInner}, Expression{Func{TOuter, TKey}}, Expression{Func{TInner, TKey}}, Expression{Func{TOuter, TInner, TResult}})" />
/// method over the given source.
/// </summary>
/// <param name="outer">The shaped query on which the operator is applied.</param>
/// <param name="inner">The inner shaped query to perform join with.</param>
/// <param name="outerKeySelector">The key selector for the outer source.</param>
/// <param name="innerKeySelector">The key selector for the inner source.</param>
/// <param name="resultSelector">The result selector supplied in the call.</param>
/// <returns>The shaped query after translation.</returns>
protected abstract ShapedQueryExpression? TranslateJoin(
ShapedQueryExpression outer,
ShapedQueryExpression inner,
LambdaExpression outerKeySelector,
LambdaExpression innerKeySelector,
LambdaExpression resultSelector);
/// <summary>
/// Translates LeftJoin over the given source.
/// </summary>
/// <remarks>
/// Certain patterns of GroupJoin-DefaultIfEmpty-SelectMany represents a left join in database. We identify such pattern
/// in advance and convert it to join like syntax.
/// </remarks>
/// <param name="outer">The shaped query on which the operator is applied.</param>
/// <param name="inner">The inner shaped query to perform join with.</param>
/// <param name="outerKeySelector">The key selector for the outer source.</param>
/// <param name="innerKeySelector">The key selector for the inner source.</param>
/// <param name="resultSelector">The result selector supplied in the call.</param>
/// <returns>The shaped query after translation.</returns>
protected abstract ShapedQueryExpression? TranslateLeftJoin(
ShapedQueryExpression outer,
ShapedQueryExpression inner,
LambdaExpression outerKeySelector,
LambdaExpression innerKeySelector,
LambdaExpression resultSelector);
/// <summary>
/// Translates LeftJoin over the given source.
/// </summary>
/// <remarks>
/// Certain patterns of GroupJoin-DefaultIfEmpty-SelectMany represents a left join in database. We identify such pattern
/// in advance and convert it to join like syntax.
/// </remarks>
/// <param name="outer">The shaped query on which the operator is applied.</param>
/// <param name="inner">The inner shaped query to perform join with.</param>
/// <param name="outerKeySelector">The key selector for the outer source.</param>
/// <param name="innerKeySelector">The key selector for the inner source.</param>
/// <param name="resultSelector">The result selector supplied in the call.</param>
/// <returns>The shaped query after translation.</returns>
protected abstract ShapedQueryExpression? TranslateRightJoin(
ShapedQueryExpression outer,
ShapedQueryExpression inner,
LambdaExpression outerKeySelector,
LambdaExpression innerKeySelector,
LambdaExpression resultSelector);
/// <summary>
/// Translates <see cref="Queryable.Last{TSource}(IQueryable{TSource})" /> method or
/// <see cref="Queryable.LastOrDefault{TSource}(IQueryable{TSource})" /> and their other overloads over the given source.
/// </summary>
/// <param name="source">The shaped query on which the operator is applied.</param>
/// <param name="predicate">The predicate supplied in the call.</param>
/// <param name="returnType">The return type of result.</param>
/// <param name="returnDefault">A value indicating whether default should be returned or throw.</param>
/// <returns>The shaped query after translation.</returns>
protected abstract ShapedQueryExpression? TranslateLastOrDefault(
ShapedQueryExpression source,
LambdaExpression? predicate,
Type returnType,
bool returnDefault);
/// <summary>
/// Translates <see cref="Queryable.LongCount{TSource}(IQueryable{TSource})" /> method and other overloads over the given source.
/// </summary>
/// <param name="source">The shaped query on which the operator is applied.</param>
/// <param name="predicate">The predicate supplied in the call.</param>
/// <returns>The shaped query after translation.</returns>
protected abstract ShapedQueryExpression? TranslateLongCount(ShapedQueryExpression source, LambdaExpression? predicate);
/// <summary>
/// Translates <see cref="Queryable.Max{TSource}(IQueryable{TSource})" /> method and other overloads over the given source.
/// </summary>
/// <param name="source">The shaped query on which the operator is applied.</param>
/// <param name="selector">The selector supplied in the call.</param>
/// <param name="resultType">The result type after the operation.</param>
/// <returns>The shaped query after translation.</returns>
protected abstract ShapedQueryExpression? TranslateMax(ShapedQueryExpression source, LambdaExpression? selector, Type resultType);
/// <summary>
/// Translates <see cref="Queryable.Min{TSource}(IQueryable{TSource})" /> method and other overloads over the given source.
/// </summary>
/// <param name="source">The shaped query on which the operator is applied.</param>
/// <param name="selector">The selector supplied in the call.</param>
/// <param name="resultType">The result type after the operation.</param>
/// <returns>The shaped query after translation.</returns>
protected abstract ShapedQueryExpression? TranslateMin(ShapedQueryExpression source, LambdaExpression? selector, Type resultType);
/// <summary>
/// Translates <see cref="Queryable.OfType{TResult}(IQueryable)" /> method over the given source.
/// </summary>
/// <param name="source">The shaped query on which the operator is applied.</param>
/// <param name="resultType">The type of result which is being filtered with.</param>
/// <returns>The shaped query after translation.</returns>
protected abstract ShapedQueryExpression? TranslateOfType(ShapedQueryExpression source, Type resultType);
/// <summary>
/// Translates <see cref="Queryable.OrderBy{TSource, TKey}(IQueryable{TSource}, Expression{Func{TSource, TKey}})" /> or
/// <see cref="Queryable.OrderByDescending{TSource, TKey}(IQueryable{TSource}, Expression{Func{TSource, TKey}})" /> method
/// over the given source.
/// </summary>
/// <param name="source">The shaped query on which the operator is applied.</param>
/// <param name="keySelector">The key selector supplied in the call.</param>
/// <param name="ascending">A value indicating whether the ordering is ascending or not.</param>
/// <returns>The shaped query after translation.</returns>
protected abstract ShapedQueryExpression? TranslateOrderBy(ShapedQueryExpression source, LambdaExpression keySelector, bool ascending);
/// <summary>
/// Translates <see cref="Queryable.Reverse{TSource}(IQueryable{TSource})" /> method over the given source.
/// </summary>
/// <param name="source">The shaped query on which the operator is applied.</param>
/// <returns>The shaped query after translation.</returns>
protected abstract ShapedQueryExpression? TranslateReverse(ShapedQueryExpression source);
/// <summary>
/// Translates <see cref="Queryable.Select{TSource, TResult}(IQueryable{TSource}, Expression{Func{TSource, TResult}})" /> method
/// over the given source.
/// </summary>
/// <param name="source">The shaped query on which the operator is applied.</param>
/// <param name="selector">The selector supplied in the call.</param>
/// <returns>The shaped query after translation.</returns>
protected abstract ShapedQueryExpression TranslateSelect(
ShapedQueryExpression source,
LambdaExpression selector);
/// <summary>
/// Translates
/// <see
/// cref="Queryable.SelectMany{TSource, TCollection, TResult}(IQueryable{TSource}, Expression{Func{TSource, IEnumerable{TCollection}}}, Expression{Func{TSource, TCollection, TResult}})" />
/// method over the given source.
/// </summary>
/// <param name="source">The shaped query on which the operator is applied.</param>
/// <param name="collectionSelector">The collection selector supplied in the call.</param>
/// <param name="resultSelector">The result selector supplied in the call.</param>
/// <returns>The shaped query after translation.</returns>
protected abstract ShapedQueryExpression? TranslateSelectMany(
ShapedQueryExpression source,
LambdaExpression collectionSelector,
LambdaExpression resultSelector);
/// <summary>
/// Translates <see cref="Queryable.SelectMany{TSource, TResult}(IQueryable{TSource}, Expression{Func{TSource, IEnumerable{TResult}}})" />
/// method over the given source.
/// </summary>
/// <param name="source">The shaped query on which the operator is applied.</param>
/// <param name="selector">The selector supplied in the call.</param>
/// <returns>The shaped query after translation.</returns>