Add support for projecting method groups

This commit is contained in:
Jayden Platell
2023-09-25 12:17:02 +10:00
parent 4ddf9f8f27
commit 0deb5ce4ad
3 changed files with 52 additions and 0 deletions

View File

@@ -41,6 +41,20 @@ namespace EntityFrameworkCore.Projectables.Services
protected override Expression VisitMethodCall(MethodCallExpression node)
{
// Replace MethodGroup arguments with their reflected expressions.
// Note that MethodCallExpression.Update returns the original Expression if argument values have not changed.
node = node.Update(node.Object, node.Arguments.Select(arg => arg switch {
UnaryExpression {
NodeType: ExpressionType.Convert,
Operand: MethodCallExpression {
NodeType: ExpressionType.Call,
Method: { Name: nameof(MethodInfo.CreateDelegate), DeclaringType.Name: nameof(MethodInfo) },
Object: ConstantExpression { Value: MethodInfo methodInfo }
}
} => TryGetReflectedExpression(methodInfo, out var expressionArg) ? expressionArg : arg,
_ => arg
}));
// Get the overriding methodInfo based on te type of the received of this expression
var methodInfo = node.Object?.Type.GetConcreteMethod(node.Method) ?? node.Method;

View File

@@ -0,0 +1,4 @@
SELECT [e].[Id], [e0].[Id] + 1, [e0].[Id]
FROM [Entity] AS [e]
LEFT JOIN [Entity] AS [e0] ON [e].[Id] = [e0].[EntityId]
ORDER BY [e].[Id]

View File

@@ -0,0 +1,34 @@
using System.Collections.Generic;
using System.Linq;
using System.Threading.Tasks;
using EntityFrameworkCore.Projectables.FunctionalTests.Helpers;
using Microsoft.EntityFrameworkCore;
using VerifyXunit;
using Xunit;
namespace EntityFrameworkCore.Projectables.FunctionalTests;
[UsesVerify]
public class MethodGroupTests
{
public record Entity
{
public int Id { get; set; }
public List<Entity>? RelatedEntities { get; set; }
}
[Projectable]
public static int NextId(Entity entity) => entity.Id + 1;
[Fact]
public Task ProjectOverMethodGroup()
{
using var dbContext = new SampleDbContext<Entity>();
var query = dbContext.Set<Entity>()
.Select(x => new { NextIds = x.RelatedEntities!.Select(NextId) });
return Verifier.Verify(query.ToQueryString());
}
}