Skip to content

Commit 0bde781

Browse files
committed
Remove duplicate code (#71)
1 parent 7330281 commit 0bde781

File tree

1 file changed

+32
-50
lines changed

1 file changed

+32
-50
lines changed

src/System.Linq.Dynamic.Core/DynamicQueryableExtensions.cs

+32-50
Original file line numberDiff line numberDiff line change
@@ -457,41 +457,19 @@ public static IQueryable GroupJoin([NotNull] this IQueryable outer, [NotNull] IE
457457
LambdaExpression outerSelectorLambda = DynamicExpressionParser.ParseLambda(createParameterCtor, outerType, null, outerKeySelector, args);
458458
LambdaExpression innerSelectorLambda = DynamicExpressionParser.ParseLambda(createParameterCtor, innerType, null, innerKeySelector, args);
459459

460-
Type outerSelectorReturnType = outerSelectorLambda.Body.Type;
461-
Type innerSelectorReturnType = innerSelectorLambda.Body.Type;
462-
463-
// If types are not the same, try to convert to Nullable and generate new LambdaExpression
464-
if (outerSelectorReturnType != innerSelectorReturnType)
465-
{
466-
if (ExpressionParser.IsNullableType(outerSelectorReturnType) && !ExpressionParser.IsNullableType(innerSelectorReturnType))
467-
{
468-
innerSelectorReturnType = ExpressionParser.ToNullableType(innerSelectorReturnType);
469-
innerSelectorLambda = DynamicExpressionParser.ParseLambda(createParameterCtor, innerType, innerSelectorReturnType, innerKeySelector, args);
470-
}
471-
else if (!ExpressionParser.IsNullableType(outerSelectorReturnType) && ExpressionParser.IsNullableType(innerSelectorReturnType))
472-
{
473-
outerSelectorReturnType = ExpressionParser.ToNullableType(outerSelectorReturnType);
474-
outerSelectorLambda = DynamicExpressionParser.ParseLambda(createParameterCtor, outerType, outerSelectorReturnType, outerKeySelector, args);
475-
}
476-
477-
// If types are still not the same, throw an Exception
478-
if (outerSelectorReturnType != innerSelectorReturnType)
479-
{
480-
throw new ParseException(string.Format(CultureInfo.CurrentCulture, Res.IncompatibleTypes, outerType, innerType), -1);
481-
}
482-
}
460+
CheckOuterAndInnerTypes(createParameterCtor, outerType, innerType, outerKeySelector, innerKeySelector, ref outerSelectorLambda, ref innerSelectorLambda, args);
483461

484462
ParameterExpression[] parameters =
485463
{
486464
Expression.Parameter(outerType, "outer"),
487-
Expression.Parameter(typeof(IEnumerable<>).MakeGenericType(inner.AsQueryable().ElementType), "inner")
465+
Expression.Parameter(typeof(IEnumerable<>).MakeGenericType(innerType), "inner")
488466
};
489467

490468
LambdaExpression resultSelectorLambda = DynamicExpressionParser.ParseLambda(createParameterCtor, parameters, null, resultSelector, args);
491469

492470
return outer.Provider.CreateQuery(Expression.Call(
493-
typeof(Queryable),
494-
"GroupJoin", new[] { outer.ElementType, innerType, outerSelectorLambda.Body.Type, resultSelectorLambda.Body.Type },
471+
typeof(Queryable), "GroupJoin",
472+
new[] { outer.ElementType, innerType, outerSelectorLambda.Body.Type, resultSelectorLambda.Body.Type },
495473
outer.Expression,
496474
Expression.Constant(inner),
497475
Expression.Quote(outerSelectorLambda),
@@ -528,29 +506,7 @@ public static IQueryable Join([NotNull] this IQueryable outer, [NotNull] IEnumer
528506
LambdaExpression outerSelectorLambda = DynamicExpressionParser.ParseLambda(createParameterCtor, outerType, null, outerKeySelector, args);
529507
LambdaExpression innerSelectorLambda = DynamicExpressionParser.ParseLambda(createParameterCtor, innerType, null, innerKeySelector, args);
530508

531-
Type outerSelectorReturnType = outerSelectorLambda.Body.Type;
532-
Type innerSelectorReturnType = innerSelectorLambda.Body.Type;
533-
534-
// If types are not the same, try to convert to Nullable and generate new LambdaExpression
535-
if (outerSelectorReturnType != innerSelectorReturnType)
536-
{
537-
if (ExpressionParser.IsNullableType(outerSelectorReturnType) && !ExpressionParser.IsNullableType(innerSelectorReturnType))
538-
{
539-
innerSelectorReturnType = ExpressionParser.ToNullableType(innerSelectorReturnType);
540-
innerSelectorLambda = DynamicExpressionParser.ParseLambda(createParameterCtor, innerType, innerSelectorReturnType, innerKeySelector, args);
541-
}
542-
else if (!ExpressionParser.IsNullableType(outerSelectorReturnType) && ExpressionParser.IsNullableType(innerSelectorReturnType))
543-
{
544-
outerSelectorReturnType = ExpressionParser.ToNullableType(outerSelectorReturnType);
545-
outerSelectorLambda = DynamicExpressionParser.ParseLambda(createParameterCtor, outerType, outerSelectorReturnType, outerKeySelector, args);
546-
}
547-
548-
// If types are still not the same, throw an Exception
549-
if (outerSelectorReturnType != innerSelectorReturnType)
550-
{
551-
throw new ParseException(string.Format(CultureInfo.CurrentCulture, Res.IncompatibleTypes, outerType, innerType), -1);
552-
}
553-
}
509+
CheckOuterAndInnerTypes(createParameterCtor, outerType, innerType, outerKeySelector, innerKeySelector, ref outerSelectorLambda, ref innerSelectorLambda, args);
554510

555511
ParameterExpression[] parameters =
556512
{
@@ -1361,8 +1317,34 @@ public static IQueryable Where([NotNull] this IQueryable source, [NotNull] strin
13611317
#endregion
13621318

13631319
#region Private Helpers
1364-
// Code below is based on https://github.com/aspnet/EntityFramework/blob/9186d0b78a3176587eeb0f557c331f635760fe92/src/Microsoft.EntityFrameworkCore/EntityFrameworkQueryableExtensions.cs
1320+
private static void CheckOuterAndInnerTypes(bool createParameterCtor, Type outerType, Type innerType, string outerKeySelector, string innerKeySelector, ref LambdaExpression outerSelectorLambda, ref LambdaExpression innerSelectorLambda, params object[] args)
1321+
{
1322+
Type outerSelectorReturnType = outerSelectorLambda.Body.Type;
1323+
Type innerSelectorReturnType = innerSelectorLambda.Body.Type;
1324+
1325+
// If types are not the same, try to convert to Nullable and generate new LambdaExpression
1326+
if (outerSelectorReturnType != innerSelectorReturnType)
1327+
{
1328+
if (ExpressionParser.IsNullableType(outerSelectorReturnType) && !ExpressionParser.IsNullableType(innerSelectorReturnType))
1329+
{
1330+
innerSelectorReturnType = ExpressionParser.ToNullableType(innerSelectorReturnType);
1331+
innerSelectorLambda = DynamicExpressionParser.ParseLambda(createParameterCtor, innerType, innerSelectorReturnType, innerKeySelector, args);
1332+
}
1333+
else if (!ExpressionParser.IsNullableType(outerSelectorReturnType) && ExpressionParser.IsNullableType(innerSelectorReturnType))
1334+
{
1335+
outerSelectorReturnType = ExpressionParser.ToNullableType(outerSelectorReturnType);
1336+
outerSelectorLambda = DynamicExpressionParser.ParseLambda(createParameterCtor, outerType, outerSelectorReturnType, outerKeySelector, args);
1337+
}
13651338

1339+
// If types are still not the same, throw an Exception
1340+
if (outerSelectorReturnType != innerSelectorReturnType)
1341+
{
1342+
throw new ParseException(string.Format(CultureInfo.CurrentCulture, Res.IncompatibleTypes, outerType, innerType), -1);
1343+
}
1344+
}
1345+
}
1346+
1347+
// Code below is based on https://github.com/aspnet/EntityFramework/blob/9186d0b78a3176587eeb0f557c331f635760fe92/src/Microsoft.EntityFrameworkCore/EntityFrameworkQueryableExtensions.cs
13661348
private static IQueryable CreateQuery(MethodInfo operatorMethodInfo, IQueryable source)
13671349
{
13681350
if (operatorMethodInfo.IsGenericMethod)

0 commit comments

Comments
 (0)