我需要在 SQL 表中查找最新生成的项目。
不幸的是,由于遗留原因,我无法按字符串列( RawValue
)以外的任何其他列进行排序。
最重要的是,我无法将此列排序为字符串,但我需要将其解析为整数。
MyEntity myEntity = await _dbContext.MyEntity
.AsNoTracking()
.Where(...) // irrelevant
.OrderByDescending(n => int.Parse(n.RawValue)).FirstOrDefaultAsync();
Entity Framework 生成了这些警告,这些警告是围绕上述语句演变的。
Microsoft.EntityFrameworkCore.Query.QueryClientEvaluationWarning" :: The LINQ expression 'orderby Parse([sn].RawValue) desc' could not be translated and will be evaluated locally.
Microsoft.EntityFrameworkCore.Query.QueryClientEvaluationWarning" :: The LINQ expression 'FirstOrDefault()' could not be translated and will be evaluated locally.
如何使用 Entity Framework 在服务器端执行该操作?
型号
public class MyEntity
{
// ctors..
public int Id { get; private set; }
[MaxLength(32)]
public string RawValue { get; private set; }
// more irrelevant properties..
}
版本信息
dotnetcoreapp2.2
EF Core 没有直接引用,但包含在 <PackageReference Include="Microsoft.AspNetCore.App" />
中
解决方案
我刚刚确认Convert.ToInt32(string)
做什么int.Parse(string)
才不是。它生成一个 ORDER BY CONVERT(int, column) ..
SQL语句。
最佳答案
很确定解决方案是使用
Convert.ToInt32
因为 Int.parse 在您使用的版本中没有实现。
关于c# - EF Core,通过从字符串进行 int 转换在 SQL 服务器上执行,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/58558588/