gpt4 book ai didi

c# - 表达式树 - Math.Max 替换

转载 作者:可可西里 更新时间:2023-11-01 08:49:37 25 4
gpt4 key购买 nike

当我使用表达式树来替换一个方法时,比如 Math.Max,看起来它在表达式树中成功地替换了它。但是当我在 Entity Framework 中使用它时,它抛出一个关于 Entity Framework 不支持 Math.Max 的异常。但我明确地替换了它。

有人知道为什么吗?以及修复代码的方法?


using System;
using System.Collections.Generic;
using System.Linq;
using System.Linq.Expressions;

namespace ConsoleApplication1
{
public static class CalculateDatabase
{
public static void Main(string[] args)
{
var calcs = GetCalculateToAmounts(GetTestItems(), 0.5m).ToList();
}

public static IQueryable<Item> GetTestItems()
{
var items = new List<Item>();
items.Add(new Item()
{
DoNotItem = true,
ReductionAmount = 2,
PreviousDiscountAmount = 3,
CurrentDiscountAmount = 10,
CurrentAmount = 100,
PreviousAmount = 50,
CurrentBillAmount = 75
});

return items.AsQueryable();
}

public class Item
{
public bool DoNotItem { get; set; }
public decimal ReductionAmount { get; set; }
public decimal PreviousDiscountAmount { get; set; }
public decimal CurrentDiscountAmount { get; set; }
public decimal CurrentAmount { get; set; }
public decimal PreviousAmount { get; set; }
public decimal CurrentBillAmount { get; set; }
}

public static IQueryable<CalculateToAmount> GetCalculateToAmounts(this IQueryable<Item> entityItems, decimal percentage)
{
return entityItems.Select(CalculateAmountExpression(percentage));
}

public class CalcType
{ }

public class CalculateToAmount
{
public CalcType CalcType { get; set; }
public Item Item { get; set; }
public decimal ItemAmount1 { get; set; }
public decimal ItemAmount2 { get; set; }
public decimal ItemAmount3 { get; set; }
public decimal Bonus { get; set; }
public decimal Discounts { get; set; }
public decimal Total { get; set; }
}

private static Expression<Func<Item, CalculateToAmount>> CalculateAmountExpression(this decimal percentage)
{
Expression<Func<Item, CalculateToAmount>> lambda = item => new CalculateToAmount()
{
Item = item,

Bonus = item.DoNotItem
? 0
: item.CurrentBillAmount * (1 - percentage) + item.ReductionAmount,
Discounts = item.PreviousDiscountAmount + item.CurrentDiscountAmount,
Total = Math.Max(item.CurrentAmount + item.PreviousAmount, item.CurrentBillAmount)
};

var test = MathModifier.Modify(lambda);
return test;
}

public class MathModifier : ExpressionVisitor
{
protected override Expression VisitMethodCall(MethodCallExpression node)
{
var isMinMethod = node.Method.Name.Equals("Min", StringComparison.InvariantCultureIgnoreCase);
var isMaxMethod = node.Method.Name.Equals("Max", StringComparison.InvariantCultureIgnoreCase);

if (!isMinMethod && !isMaxMethod)
return base.VisitMethodCall(node);

var left = node.Arguments[0];
var right = node.Arguments[1];

var minMaxReplaceMethod =
isMinMethod
? Expression.Condition(Expression.LessThan(left, right), left, right)
: Expression.Condition(Expression.GreaterThan(left, right), left, right);

return minMaxReplaceMethod;
}

public static Expression<Func<TIn, TOut>> Modify<TIn, TOut>(Expression<Func<TIn, TOut>> expression)
{
var modifier = new MathModifier();
return (Expression<Func<TIn, TOut>>)modifier.Visit(expression);
}
}
}
}

如果你打电话

var calcs = GetCalculateToAmounts(GetTestItems(), 0.5).ToList()

它会起作用的。但是如果你把上面的GetTestItems()换成 Entity Framework _dbContext.Items,就不行了。

要测试此代码,您需要将 Item 结构添加到 EF 项目,进行迁移并将其推送到数据库中。

我希望我能减少这个技术性的问题,这样这个问题就可以被更广泛的人回答。希望赏金足以回答。如果没有,请私信我。

最佳答案

我正在 完全 使用 EF 6 运行您提供的代码。名为 Table1 的实体 Item。代码运行成功,Math.Max 的替换成功完成。

使用它生成的 SQL Profiler 跟踪 SQL,并针对数据库发送以下 SQL:

exec sp_executesql N'SELECT 
[Extent1].[Id] AS [Id],
[Extent1].[DoNotItem] AS [DoNotItem],
[Extent1].[ReductionAmount] AS [ReductionAmount],
[Extent1].[PreviousDiscountAmount] AS [PreviousDiscountAmount],
[Extent1].[CurrentDiscountAmount] AS [CurrentDiscountAmount],
[Extent1].[CurrentAmount] AS [CurrentAmount],
[Extent1].[PreviousAmount] AS [PreviousAmount],
[Extent1].[CurrentBillAmount] AS [CurrentBillAmount],
CASE WHEN ([Extent1].[DoNotItem] = 1) THEN cast(0 as decimal(18)) ELSE ([Extent1].[CurrentBillAmount] * (cast(1 as decimal(18)) - @p__linq__0)) + [Extent1].[ReductionAmount] END AS [C1],
[Extent1].[PreviousDiscountAmount] + [Extent1].[CurrentDiscountAmount] AS [C2],
CASE WHEN (([Extent1].[CurrentAmount] + [Extent1].[PreviousAmount]) > [Extent1].[CurrentBillAmount]) THEN [Extent1].[CurrentAmount] + [Extent1].[PreviousAmount] ELSE [Extent1].[CurrentBillAmount] END AS [C3]
FROM [dbo].[Table1] AS [Extent1]',N'@p__linq__0 decimal(1,1)',@p__linq__0=0.5

结论:您提供的代码有效,表达式树操作有效,树中的条件逻辑已转换为 SQL CASE 表达式。

可能您的 EF 测试隔离得不够仔细,并且与 Max.Math 相关的 EF 异常来自代码的其他部分和/或间接来自已触发的事件、过载等。

关于c# - 表达式树 - Math.Max 替换,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/31719225/

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