当我使用表达式树来替换方法(例如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()
它会起作用。但是如果用实体框架_dbContext.Items替换上面的GetTestItems(),它将无法工作。
要测试此代码,您需要将项目结构添加到EF项目,进行迁移,然后将其推送到数据库中。
我希望我能减少这种技术,所以这个问题可以由更多的人来回答。希望赏金足以得到答案。如果没有,请发给我。
答案 0 :(得分:4)
我正在使用EF 6运行完全您提供的代码。名为Table1的实体Item
。代码运行成功,Math.Max的重新分区成功完成。
使用SQL事件探查器跟踪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异常来自代码的其他部分和/或间接来自被触发的事件,过载等。
答案 1 :(得分:-3)
你有没有理由把它遗留在IQueryable
土地上? IQueryable
land使一切都在数据库上运行。如果你把它移到IEnumerable
土地上,我认为应该可以正常工作:
public static IEnumerable<CalculateToAmount> GetCalculateToAmounts(this IQueryable<Item> entityItems, decimal percentage)
{
var func = CalculateAmountExpression(percentage).Compile();
return entityItems.AsEnumerable().Select(func);
}