How to parameterize a selector with a function in EF query?(如何在 EF 查询中使用函数参数化选择器?)
问题描述
我有一个投影函数,我传递给 IQueryable<>.Select()
方法:
I have a projection function that I pass to IQueryable<>.Select()
method:
private static Expression<Func<VendorPrice, PriceItem>> GetPriceSelector(){
return e => new PriceItem {
Id = e.Id,
Price = Math.Round(e.Price, 4)
};
}
一切正常,但我想像这样参数化它:
Everything works just fine but I want to parameterize it like that:
private static Expression<Func<VendorPrice, PriceItem>> GetPriceSelector(Func<VendorPrice, decimal> formula){
return e => new PriceItem {
Id = e.Id,
Price = formula(e)
};
}
所以我可以这样称呼它
prices.Select(GetPriceSelector(e => Math.Round(e.Price, 4)))
不幸的是,EF 抱怨它:
Unfortunately, EF complains about it:
LINQ 不支持 LINQ 表达式节点类型Invoke"实体
The LINQ expression node type 'Invoke' is not supported in LINQ to Entities
如何重写代码让EF开心?
How to rewrite the code to make EF happy?
推荐答案
首先,GetPriceSelector
方法需要接受一个表达式,而不是一个函数.不同之处在于,表达式是作为数据的代码,因此可以转换为 SQL,而函数是编译后的代码,因此无法转换为 SQL.
First, the GetPriceSelector
method needs to take in an expression, not a function. The difference is that an expression is code as data so it can be translated to SQL, while a function is compiled code so it cannot be translated to SQL.
接下来,您需要一种方法来合并这两个表达式.手动执行此操作很困难.幸运的是,有一个名为 LINQKit 的库可以做到这一点.以下是使用 LINQKit 解决问题的方法:
Next, you need a way to merge the two expressions. Doing this manually is hard. Fortunately, there is a library called LINQKit that can do that. Here is how you can solve your problem with LINQKit:
private static Expression<Func<VendorPrice, PriceItem>> GetPriceSelector(
Expression<Func<VendorPrice, decimal>> formula)
{
Expression<Func<VendorPrice, PriceItem>> expression = e => new PriceItem
{
Id = e.Id,
Price = formula.Invoke(e) //use the forumla expression here
};
return expression.Expand(); //This causes formula.Invoke(e) to be converted
//to something like Math.Round(e.Price, 4)
}
这篇关于如何在 EF 查询中使用函数参数化选择器?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持编程学习网!
本文标题为:如何在 EF 查询中使用函数参数化选择器?
基础教程推荐
- c# Math.Sqrt 实现 2022-01-01
- 如何激活MC67中的红灯 2022-01-01
- 如何在 IDE 中获取 Xamarin Studio C# 输出? 2022-01-01
- SSE 浮点算术是否可重现? 2022-01-01
- 将 XML 转换为通用列表 2022-01-01
- 将 Office 安装到 Windows 容器 (servercore:ltsc2019) 失败,错误代码为 17002 2022-01-01
- MS Visual Studio .NET 的替代品 2022-01-01
- 为什么Flurl.Http DownloadFileAsync/Http客户端GetAsync需要 2022-09-30
- rabbitmq 的 REST API 2022-01-01
- 有没有办法忽略 2GB 文件上传的 maxRequestLength 限制? 2022-01-01