Skip to content

Commit 3fae43f

Browse files
committed
Tiny fixup to #36232
1 parent 6a6249c commit 3fae43f

File tree

3 files changed

+91
-83
lines changed

3 files changed

+91
-83
lines changed

src/EFCore.Relational/Query/RelationalQueryableMethodTranslatingExpressionVisitor.CreateSelect.cs

Lines changed: 15 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -700,26 +700,26 @@ private static ColumnExpression CreateColumnExpression(
700700
string tableAlias,
701701
bool nullable)
702702
=> new(
703-
column,
703+
column.Name,
704704
tableAlias,
705+
column,
705706
property.ClrType.UnwrapNullableType(),
706707
column.PropertyMappings.First(m => m.Property == property).TypeMapping,
707-
nullable);
708+
nullable || column.IsNullable);
708709

709710
private static ColumnExpression CreateColumnExpression(ProjectionExpression subqueryProjection, string tableAlias)
710-
=> subqueryProjection.Expression is ColumnExpression { Column: IColumnBase column, TypeMapping: RelationalTypeMapping typeMapping } columnExpression
711-
? new(column, tableAlias, columnExpression.Type, typeMapping, columnExpression.IsNullable)
712-
: new(
713-
subqueryProjection.Alias,
714-
tableAlias,
715-
subqueryProjection.Type,
716-
subqueryProjection.Expression.TypeMapping!,
717-
subqueryProjection.Expression switch
718-
{
719-
ColumnExpression c => c.IsNullable,
720-
SqlConstantExpression c => c.Value is null,
721-
_ => true
722-
});
711+
=> new(
712+
subqueryProjection.Alias,
713+
tableAlias,
714+
column: subqueryProjection.Expression is ColumnExpression { Column: IColumnBase column } ? column : null,
715+
subqueryProjection.Type,
716+
subqueryProjection.Expression.TypeMapping!,
717+
subqueryProjection.Expression switch
718+
{
719+
ColumnExpression c => c.IsNullable,
720+
SqlConstantExpression c => c.Value is null,
721+
_ => true
722+
});
723723

724724
private static ColumnExpression CreateColumnExpression(
725725
TableExpressionBase tableExpression,

src/EFCore.Relational/Query/SqlExpressions/ColumnExpression.cs

Lines changed: 9 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -31,33 +31,32 @@ public ColumnExpression(
3131
Type type,
3232
RelationalTypeMapping? typeMapping,
3333
bool nullable)
34-
: base(type, typeMapping)
34+
: this(name, tableAlias, column: null, type, typeMapping, nullable)
3535
{
36-
Name = name;
37-
TableAlias = tableAlias;
38-
IsNullable = nullable;
3936
}
4037

4138
/// <summary>
4239
/// Creates a new instance of the <see cref="ColumnExpression" /> class.
4340
/// </summary>
44-
/// <param name="column">The <see cref="IColumnBase" /> associated with this column expression.</param>
41+
/// <param name="name">The name of the column.</param>
4542
/// <param name="tableAlias">The alias of the table to which this column refers.</param>
43+
/// <param name="column">An optional <see cref="IColumnBase" /> associated with this column expression.</param>
4644
/// <param name="type">The <see cref="System.Type" /> of the expression.</param>
4745
/// <param name="typeMapping">The <see cref="RelationalTypeMapping" /> associated with the expression.</param>
4846
/// <param name="nullable">Whether this expression represents a nullable column.</param>
49-
[EntityFrameworkInternal]
5047
public ColumnExpression(
51-
IColumnBase column,
48+
string name,
5249
string tableAlias,
50+
IColumnBase? column,
5351
Type type,
54-
RelationalTypeMapping typeMapping,
52+
RelationalTypeMapping? typeMapping,
5553
bool nullable)
5654
: base(type, typeMapping)
5755
{
58-
Name = column.Name;
56+
Name = name;
5957
TableAlias = tableAlias;
60-
IsNullable = nullable || column.IsNullable;
58+
Column = column;
59+
IsNullable = nullable;
6160
}
6261

6362
/// <summary>

src/EFCore.Relational/Query/SqlExpressions/SelectExpression.cs

Lines changed: 67 additions & 58 deletions
Original file line numberDiff line numberDiff line change
@@ -1232,64 +1232,73 @@ Expression CopyProjectionToOuter(SelectExpression innerSelectExpression, Express
12321232
{
12331233
var constantValue = ((ConstantExpression)innerSelectExpression._clientProjections[j]).Value!;
12341234
ConstantExpression remappedConstant;
1235-
if (constantValue is Dictionary<IProperty, int> entityDictionary)
1235+
switch (((ConstantExpression)innerSelectExpression._clientProjections[j]).Value!)
12361236
{
1237-
var newDictionary = new Dictionary<IProperty, int>(entityDictionary.Count);
1238-
foreach (var (property, value) in entityDictionary)
1237+
case Dictionary<IProperty, int> entityDictionary:
12391238
{
1240-
newDictionary[property] = projectionIndexMap[value];
1241-
}
1242-
1243-
remappedConstant = Constant(newDictionary);
1244-
}
1245-
else if (constantValue is JsonProjectionInfo jsonProjectionInfo)
1246-
{
1247-
var newKeyAccessInfo = new List<(IProperty?, int?, int?)>();
1248-
foreach (var (keyProperty, constantKeyValue, keyProjectionIndex) in jsonProjectionInfo.KeyAccessInfo)
1249-
{
1250-
newKeyAccessInfo.Add(
1251-
(keyProperty, constantKeyValue,
1252-
keyProjectionIndex != null ? projectionIndexMap[keyProjectionIndex.Value] : null));
1253-
}
1239+
var newDictionary = new Dictionary<IProperty, int>(entityDictionary.Count);
1240+
foreach (var (property, value) in entityDictionary)
1241+
{
1242+
newDictionary[property] = projectionIndexMap[value];
1243+
}
12541244

1255-
remappedConstant = Constant(
1256-
new JsonProjectionInfo(
1257-
projectionIndexMap[jsonProjectionInfo.JsonColumnIndex],
1258-
newKeyAccessInfo));
1259-
}
1260-
else if (constantValue is QueryableJsonProjectionInfo queryableJsonProjectionInfo)
1261-
{
1262-
var newPropertyIndexMap = new Dictionary<IProperty, int>(queryableJsonProjectionInfo.PropertyIndexMap.Count);
1263-
foreach (var (property, value) in queryableJsonProjectionInfo.PropertyIndexMap)
1264-
{
1265-
newPropertyIndexMap[property] = projectionIndexMap[value];
1245+
remappedConstant = Constant(newDictionary);
1246+
break;
12661247
}
12671248

1268-
var newChildrenProjectionInfo = new List<(JsonProjectionInfo, INavigation)>();
1269-
foreach (var childProjectionInfo in queryableJsonProjectionInfo.ChildrenProjectionInfo)
1249+
case JsonProjectionInfo jsonProjectionInfo:
12701250
{
12711251
var newKeyAccessInfo = new List<(IProperty?, int?, int?)>();
1272-
foreach (var (keyProperty, constantKeyValue, keyProjectionIndex) in childProjectionInfo.JsonProjectionInfo
1273-
.KeyAccessInfo)
1252+
foreach (var (keyProperty, constantKeyValue, keyProjectionIndex) in jsonProjectionInfo.KeyAccessInfo)
12741253
{
12751254
newKeyAccessInfo.Add(
12761255
(keyProperty, constantKeyValue,
12771256
keyProjectionIndex != null ? projectionIndexMap[keyProjectionIndex.Value] : null));
12781257
}
12791258

1280-
newChildrenProjectionInfo.Add(
1281-
(new JsonProjectionInfo(
1282-
projectionIndexMap[childProjectionInfo.JsonProjectionInfo.JsonColumnIndex],
1283-
newKeyAccessInfo),
1284-
childProjectionInfo.Navigation));
1259+
remappedConstant = Constant(
1260+
new JsonProjectionInfo(
1261+
projectionIndexMap[jsonProjectionInfo.JsonColumnIndex],
1262+
newKeyAccessInfo));
1263+
break;
12851264
}
12861265

1287-
remappedConstant = Constant(
1288-
new QueryableJsonProjectionInfo(newPropertyIndexMap, newChildrenProjectionInfo));
1289-
}
1290-
else
1291-
{
1292-
remappedConstant = Constant(projectionIndexMap[(int)constantValue]);
1266+
case QueryableJsonProjectionInfo queryableJsonProjectionInfo:
1267+
{
1268+
var newPropertyIndexMap = new Dictionary<IProperty, int>(queryableJsonProjectionInfo.PropertyIndexMap.Count);
1269+
foreach (var (property, value) in queryableJsonProjectionInfo.PropertyIndexMap)
1270+
{
1271+
newPropertyIndexMap[property] = projectionIndexMap[value];
1272+
}
1273+
1274+
var newChildrenProjectionInfo = new List<(JsonProjectionInfo, INavigation)>();
1275+
foreach (var childProjectionInfo in queryableJsonProjectionInfo.ChildrenProjectionInfo)
1276+
{
1277+
var newKeyAccessInfo = new List<(IProperty?, int?, int?)>();
1278+
foreach (var (keyProperty, constantKeyValue, keyProjectionIndex) in childProjectionInfo.JsonProjectionInfo
1279+
.KeyAccessInfo)
1280+
{
1281+
newKeyAccessInfo.Add(
1282+
(keyProperty, constantKeyValue,
1283+
keyProjectionIndex != null ? projectionIndexMap[keyProjectionIndex.Value] : null));
1284+
}
1285+
1286+
newChildrenProjectionInfo.Add(
1287+
(new JsonProjectionInfo(
1288+
projectionIndexMap[childProjectionInfo.JsonProjectionInfo.JsonColumnIndex],
1289+
newKeyAccessInfo),
1290+
childProjectionInfo.Navigation));
1291+
}
1292+
1293+
remappedConstant = Constant(
1294+
new QueryableJsonProjectionInfo(newPropertyIndexMap, newChildrenProjectionInfo));
1295+
1296+
break;
1297+
}
1298+
1299+
default:
1300+
remappedConstant = Constant(projectionIndexMap[(int)constantValue]);
1301+
break;
12931302
}
12941303

12951304
newClientProjections.Add(remappedConstant);
@@ -4031,26 +4040,26 @@ private static ColumnExpression CreateColumnExpression(
40314040
string tableAlias,
40324041
bool nullable)
40334042
=> new(
4034-
column,
4043+
column.Name,
40354044
tableAlias,
4045+
column,
40364046
property.ClrType.UnwrapNullableType(),
40374047
column.PropertyMappings.First(m => m.Property == property).TypeMapping,
4038-
nullable);
4048+
nullable || column.IsNullable);
40394049

40404050
private static ColumnExpression CreateColumnExpression(ProjectionExpression subqueryProjection, string tableAlias)
4041-
=> subqueryProjection.Expression is ColumnExpression { Column: IColumnBase column, TypeMapping: RelationalTypeMapping typeMapping } columnExpression
4042-
? new(column, tableAlias, columnExpression.Type, typeMapping, columnExpression.IsNullable)
4043-
: new(
4044-
subqueryProjection.Alias,
4045-
tableAlias,
4046-
subqueryProjection.Type,
4047-
subqueryProjection.Expression.TypeMapping!,
4048-
subqueryProjection.Expression switch
4049-
{
4050-
ColumnExpression c => c.IsNullable,
4051-
SqlConstantExpression c => c.Value is null,
4052-
_ => true
4053-
});
4051+
=> new(
4052+
subqueryProjection.Alias,
4053+
tableAlias,
4054+
column: subqueryProjection.Expression is ColumnExpression { Column: IColumnBase column } ? column : null,
4055+
subqueryProjection.Type,
4056+
subqueryProjection.Expression.TypeMapping!,
4057+
subqueryProjection.Expression switch
4058+
{
4059+
ColumnExpression c => c.IsNullable,
4060+
SqlConstantExpression c => c.Value is null,
4061+
_ => true
4062+
});
40544063

40554064
private ColumnExpression GenerateOuterColumn(
40564065
string tableAlias,

0 commit comments

Comments
 (0)