【发布时间】:2020-03-06 22:48:54
【问题描述】:
var query = db.Products.Where(p => p.Cost == 10M); 可以正常工作并返回正确的结果列表。
var query = db.Products.Where(p => p.Cost > 10M); 抛出异常:
The LINQ expression 'DbSet<Product>.Where(p => p.Cost > (Nullable<decimal>)10)' could not be translated.
显然C#生成的脚本不能在SQL中执行,但为什么不能呢?这是一个有效的 SQL 查询
public class Product
{
public int ProductID {get;set;}
[Required]
[StringLength(40)]
public string ProductName {get;set;}
[Column("UnitPrice", TypeName="money")]
public decimal? Cost {get;set;}
}
public class Northwind: DbContext
{
public DbSet<Category> Categories {get;set;}
public DbSet<Product> Products {get;set;}
}
EFCore 3.1.2, .NET Core 3.1.1
【问题讨论】:
标签: c# linq entity-framework-core