gpt4 book ai didi

c# - EF Core 的 ConstantExpression 中的正确集合

转载 作者:行者123 更新时间:2023-12-02 02:58:17 26 4
gpt4 key购买 nike

我尝试实现自己的表达式序列化器/反序列化器,以便通过服务传递它(我想实现我自己的 EF Core 服务端点)。所以,现在我对 LambdaExpressions 中的集合有问题。例如,

var dataQuery = testDb.Users.Include(e => e.EmployeeInfo).Include(f => f.Notifications).Where(s => tstList.Contains(s.Id)).Select(e => e.FullName);
var tstEspressionBase = dataQuery.Expression;
var tstEspression = new ReflectionLocalValculationVisitor().Visit(tstEspressionBase);

这里

public class ReflectionLocalValculationVisitor : ExpressionVisitor
{
protected override Expression VisitMember(MemberExpression memberExpression)
{
var expression = Visit(memberExpression.Expression);

if (expression is ConstantExpression)
{
object container = ((ConstantExpression)expression).Value;
var member = memberExpression.Member;
if (member is FieldInfo)
{
object value = ((FieldInfo)member).GetValue(container);
return Expression.Constant(value);
}
if (member is PropertyInfo)
{
object value = ((PropertyInfo)member).GetValue(container, null);
return Expression.Constant(value);
}
}
return base.VisitMember(memberExpression);
}
}

var tstList = new List<Guid>()
{
new Guid("D45E1A1A-F546-48DB-77BA-08D7775C6A93"),
new Guid("5B21C782-9B95-48F2-77BD-08D7775C6A93")
};

使用此代码执行

var providerAsync = testDb.GetService<IAsyncQueryProvider>();
var toListAsyncMethodInfo = typeof(EntityFrameworkQueryableExtensions).GetMethod(nameof(EntityFrameworkQueryableExtensions.ToListAsync)).MakeGenericMethod(typeof(string));
var s3 = await toListAsyncMethodInfo.InvokeAsync(null, new object[] { providerAsync.CreateQuery(tstEspression), default(CancellationToken) }).ConfigureAwait(false);

给了我正确的结果。

因此,在使用 Newtonsoft Json 进行序列化/反序列化后,我在 Lambda 中的集合出现问题。来自Where方法:

Error Message: The LINQ expression 'DbSet .Where(u => List { d45e1a1a-f546-48db-77ba-08d7775c6a93, 5b21c782-9b95-48f2-77bd-08d7775c6a93, }.Contains(s.Id))' could not be translated. Either rewrite the query in a form that can be translated, or switch to client evaluation explicitly by inserting a call to either AsEnumerable(), AsAsyncEnumerable(), ToList(), or ToListAsync(). See https://go.microsoft.com/fwlink/?linkid=2101038 for more information.

我试图实现这个“建议”,但没有效果(见下面的代码):

var asEnumerableMethod = typeof(Enumerable).GetMethod(nameof(Enumerable.AsEnumerable)).MakeGenericMethod(GenericTypes.Select(e => e.FromNode()).ToArray());
var asEnumerabled = asEnumerableMethod.Invoke(null, new object[] { Value });

这里Value对象是 List<Guid>由 JSON.NET 反序列化后生成。因此,我比较了 Value 的实现接口(interface)在ConstantExpression代表List<guid>序列化之前和反序列化之后 - 都实现 8 个接口(interface)。

所以,也许有人遇到了同样的问题。

谢谢。

附注我不知道为什么 EF Core 给我 Where(u => ...而不是Where(s => ... ,因为在这个表达式的 DebugView 模式下我看到正确的 Where(s => ...代表。

让我们看看序列化/(反序列化和恢复)表达式(来自 DebugView 的数据):

.Call System.Linq.Queryable.Select(
.Call System.Linq.Queryable.Where(
.Call Microsoft.EntityFrameworkCore.EntityFrameworkQueryableExtensions.Include(
.Call Microsoft.EntityFrameworkCore.EntityFrameworkQueryableExtensions.Include(
.Constant<Microsoft.EntityFrameworkCore.Query.Internal.EntityQueryable`1[EFCoreDataModel.DataClasses.Users.Base.User]>(Microsoft.EntityFrameworkCore.Query.Internal.EntityQueryable`1[EFCoreDataModel.DataClasses.Users.Base.User]),
'(.Lambda #Lambda1<System.Func`2[EFCoreDataModel.DataClasses.Users.Base.User,EFCoreDataModel.DataClasses.Users.Employ.EmployeeInfo]>))
,
'(.Lambda #Lambda2<System.Func`2[EFCoreDataModel.DataClasses.Users.Base.User,System.Collections.Generic.ICollection`1[EFCoreDataModel.DataClasses.Notifications.Notification]]>))
,
'(.Lambda #Lambda3<System.Func`2[EFCoreDataModel.DataClasses.Users.Base.User,System.Boolean]>)),
'(.Lambda #Lambda4<System.Func`2[EFCoreDataModel.DataClasses.Users.Base.User,System.String]>))

.Lambda #Lambda1<System.Func`2[EFCoreDataModel.DataClasses.Users.Base.User,EFCoreDataModel.DataClasses.Users.Employ.EmployeeInfo]>(EFCoreDataModel.DataClasses.Users.Base.User $e)
{
$e.EmployeeInfo
}

.Lambda #Lambda2<System.Func`2[EFCoreDataModel.DataClasses.Users.Base.User,System.Collections.Generic.ICollection`1[EFCoreDataModel.DataClasses.Notifications.Notification]]>(EFCoreDataModel.DataClasses.Users.Base.User $f)
{
$f.Notifications
}

.Lambda #Lambda3<System.Func`2[EFCoreDataModel.DataClasses.Users.Base.User,System.Boolean]>(EFCoreDataModel.DataClasses.Users.Base.User $s)
{
.Call .Constant<System.Collections.Generic.List`1[System.Guid]>(System.Collections.Generic.List`1[System.Guid]).Contains($s.Id)
}

.Lambda #Lambda4<System.Func`2[EFCoreDataModel.DataClasses.Users.Base.User,System.String]>(EFCoreDataModel.DataClasses.Users.Base.User $e)
{
$e.FullName
}

原始表达式(来自 DebugView):

.Call System.Linq.Queryable.Select(
.Call System.Linq.Queryable.Where(
.Call Microsoft.EntityFrameworkCore.EntityFrameworkQueryableExtensions.Include(
.Call Microsoft.EntityFrameworkCore.EntityFrameworkQueryableExtensions.Include(
.Constant<Microsoft.EntityFrameworkCore.Query.Internal.EntityQueryable`1[EFCoreDataModel.DataClasses.Users.Base.User]>(Microsoft.EntityFrameworkCore.Query.Internal.EntityQueryable`1[EFCoreDataModel.DataClasses.Users.Base.User]),
'(.Lambda #Lambda1<System.Func`2[EFCoreDataModel.DataClasses.Users.Base.User,EFCoreDataModel.DataClasses.Users.Employ.EmployeeInfo]>))
,
'(.Lambda #Lambda2<System.Func`2[EFCoreDataModel.DataClasses.Users.Base.User,System.Collections.Generic.ICollection`1[EFCoreDataModel.DataClasses.Notifications.Notification]]>))
,
'(.Lambda #Lambda3<System.Func`2[EFCoreDataModel.DataClasses.Users.Base.User,System.Boolean]>)),
'(.Lambda #Lambda4<System.Func`2[EFCoreDataModel.DataClasses.Users.Base.User,System.String]>))

.Lambda #Lambda1<System.Func`2[EFCoreDataModel.DataClasses.Users.Base.User,EFCoreDataModel.DataClasses.Users.Employ.EmployeeInfo]>(EFCoreDataModel.DataClasses.Users.Base.User $e)
{
$e.EmployeeInfo
}

.Lambda #Lambda2<System.Func`2[EFCoreDataModel.DataClasses.Users.Base.User,System.Collections.Generic.ICollection`1[EFCoreDataModel.DataClasses.Notifications.Notification]]>(EFCoreDataModel.DataClasses.Users.Base.User $f)
{
$f.Notifications
}

.Lambda #Lambda3<System.Func`2[EFCoreDataModel.DataClasses.Users.Base.User,System.Boolean]>(EFCoreDataModel.DataClasses.Users.Base.User $s)
{
.Call .Constant<System.Collections.Generic.List`1[System.Guid]>(System.Collections.Generic.List`1[System.Guid]).Contains($s.Id)
}

.Lambda #Lambda4<System.Func`2[EFCoreDataModel.DataClasses.Users.Base.User,System.String]>(EFCoreDataModel.DataClasses.Users.Base.User $e)
{
$e.FullName
}

所以,它们是平等的。并且序列化/反序列化没有u Lambda 中的参数,可能只是“s”。

最佳答案

是的,当您使用自己的自定义集合作为 where 表达式中的常量时,EF Core 无法正常运行。它们必须在表达式中完全可计算,即使这样,EF Core 也很难正确翻译它们。也许您想简化您的 list ?我的意思是,您为每个用户创建一个表达式,将每个项目与其进行比较。

我已经实现了一个用于集合的小型谓词构建器,它可能会对您有帮助吗?它负责所有 ParameterExpression 映射。

var whereExpression = CollectionConstantPredicateBuilder<UserEntity>
// Here begins the scope of ALL users.
.CreateFromCollection(tstList)
// Each expression of one user is combined with OrElse (||)
.DefinePredicatePerItem(consecutiveItemBinaryExpressionFactory: Expression.OrElse,
// Pre-Conditions, you may check for null or empty list or you simply configures
// comparisonValuesBehaviourFlags. Do NOT use method call on 'yourTstList'.
sourceAndItemPredicate: (user, yourTstList) => true)
// Here begins the scope of ALL items of 'yourTstList' in ONE user.
// The resulted expression of the hole collection that belongs to one user
// is combined with the previous expression from ONE user by AndAlso (&&).
.ThenCreateFromCollection(parentBinaryExpressionFactory: Expression.AndAlso,
comparisonValuesFactory: yourTstList => yourTstList,
// If the collection of the comparison values is null or empty,
// the boolean expression branch for each each item won't be
// created. Instead an expression is used that leads to false.
// Ergo, if 'yourTstList' does not contain the user id, the user
// won't be queried.
comparisonValuesBehaviourFlags: ComparisonValuesBehaviourFlags.NullOrEmptyLeadsToFalse)
// Each expression of one item is combined with OrElse (||).
// So one user's ID can be the one or the other 'oneTstItem'.
.DefinePredicatePerItem(Expression.OrElse,
sourceAndItemPredicate: (user, oneTstItem) => user.Id == oneTstItem)
.BuildLambdaExpression();

dbContext.Users.AsQueryable().Where(whereExpression).

你可以在我的 pre-release package (0.1.7-alpha.68) 中找到 CollectionConstantPredicateBuilder上NuGet .

关于c# - EF Core 的 ConstantExpression 中的正确集合,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/60590444/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com