mirror of
https://github.com/zoriya/EntityFrameworkCore.Projectables.git
synced 2025-12-06 05:56:10 +00:00
Add support for projecting method groups
This commit is contained in:
@@ -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;
|
||||
|
||||
|
||||
@@ -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]
|
||||
@@ -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());
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user