升级到EF Core 3后,我将在以下代码中得到以下错误:
Convert.ToInt32(c.ClaimNumber.Substring(c.ClaimNumber.Length System.InvalidOperationException:‘LINQ表达式'DbSet .Max(c => .Max-6)’)无法翻译。要么用可以翻译的表单重写查询,要么通过插入对AsEnumerable()、AsAsyncEnumerable()、ToList()或ToListAsync()的调用,显式地切换到客户端计算。更多信息请参见https://go.microsoft.com/fwlink/?linkid=2101038。
var maxId = Db.Claims
.Select(c => c.ClaimNumber.Substring(c.ClaimNumber.Length - 6))
.Max(x => Convert.ToInt32(x));我也尝试过使用int.Parse而不是Convert.ToInt32,并且它会产生同样的错误。我理解错误信息。但是,要让Server在that中使用强制转换或转换将字符串解析为int非常简单,我希望有一种简单的方法来编写查询,以便将其转换为服务器端的操作,对吗?
在克劳迪奥的出色回答之后更新了,我想我应该为下一个出现的人添加一些信息。我认为解析是上述代码的问题所在,是因为以下代码运行时没有出错,并产生了正确的结果:
var maxId = Db.Claims
.Select(c => c.ClaimNumber.Substring(c.ClaimNumber.Length - 6))
.AsEnumerable()
.Max(x => int.Parse(x));但是,我深入研究发现,这是SQL查询EF从该代码中执行的:
SELECT [c].[ClaimNumber], CAST(LEN([c].[ClaimNumber]) AS int) - 6
FROM [Claims] AS [c]
WHERE [c].[ClaimNumber] IS NOT NULL这显然不是我想做的事情,因此克劳迪奥是对的,对Substring的调用实际上就是问题所在。
发布于 2020-03-03 00:13:34
免责声明:我强烈建议您不要在查询中使用类型转换,因为这会导致严重的查询性能下降。
事实上,Convert.ToInt(x)部分不是这里的问题。是c.ClaimsNumber.Substring(c.ClaimNumber.Length - 6),EF翻译器无法在T中进行翻译.
尽管Server中存在RIGHT函数,但是您将无法在当前版本的EF中使用它(目前我正在编写的最后一个版本是3.1.2 )。获得所需内容的唯一解决方案是创建Server用户函数,用EF Core映射它,并在查询中使用它。
1)通过迁移创建函数
> dotnet ef migrations add CreateRightFunction在新创建的迁移文件中,放入以下代码:
public partial class CreateRightFunctions : Migration
{
protected override void Up(MigrationBuilder migrationBuilder)
{
migrationBuilder.Sql(@"
CREATE FUNCTION fn_Right(@input nvarchar(4000), @howMany int)
RETURNS nvarchar(4000)
BEGIN
RETURN RIGHT(@input, @howMany)
END
");
}
protected override void Down(MigrationBuilder migrationBuilder)
{
migrationBuilder.Sql(@"
DROP FUNCTION fn_Right
");
}
}然后运行db更新:
dotnet ef database update2)映射函数到EF核心上下文
在上下文中classDbFunction("fn_Right")
public static string Right(string input, int howMany)
{
throw new NotImplementedException(); // this code doesn't get executed; the call is passed through to the database function
}3)在查询中使用函数
var maxId = Db.Claims.Select(c => MyContext.Right(c.ClaimNumber, 6)).Max(x => Convert.ToInt32(x));生成的查询:
SELECT MAX(CONVERT(int, [dbo].[fn_Right]([c].[ClaimNumber], 6)))
FROM [Claims] AS [c]同样,这与最佳实践相去甚远,我认为您应该考虑在表中添加一个int列来存储这个“数字”,不管它在您的域中代表什么。
另外,当ClaimNumber的最后6个字符包含一个非数字字符时,这将不再起作用。如果ClaimNumber是由人类输入的,迟早会发生这种情况。
您应该为健壮性编写和设计数据库和应用程序,即使您非常确信这6个字符总是代表一个数字。他们不能永远这样做:)
发布于 2021-10-06 15:11:32
请按以下方式更改您的代码。它适用于我的Dotnet核心3.1版本
var maxId = Db.Claims.Select(c => c.ClaimNumber.Substring(c.ClaimNumber.Length - 6))
.Max(x => (Convert.ToInt32((x == null)? "0" : x.ToString())));https://stackoverflow.com/questions/60497605
复制相似问题