【发布时间】:2019-01-19 21:32:06
【问题描述】:
我有这段代码用于从数据库中获取和过滤产品结果:
if (userParams.MinPrice > 0 && userParams.MaxPrice != 999999999)
{
products = products.Where(p => (p.Price >= userParams.MinPrice) &&
(p.Price <= userParams.MaxPrice));
}
仅提供userParams.MinPrice 时执行成功,但提供userParams.MaxPrice 时执行失败。
我得到的错误是这样的。
执行请求时发生未处理的异常。
System.InvalidOperationException:尝试评估 LINQ 查询参数表达式时引发异常。要显示更多信息,请在覆盖 DbContext.OnConfiguring 时调用 EnableSensitiveDataLogging()。
System.NullReferenceException:对象引用未设置为实例 的一个对象。
在 lambda_method(闭包)
在 Microsoft.EntityFrameworkCore.Query.ExpressionVisitors.Internal.ParameterExtractingExpressionVisitor.Evaluate(表达式表达式,字符串和参数名称)。
有人知道这个问题的解决方案吗?请帮忙
【问题讨论】:
-
最高价格为空。 if (userParams.MinPrice != null && userParams.MinPrice > 0 && userParams.MaxPrice != null && userParams.MaxPrice != 999999999)
-
MaxPrice 不为空,因为如果用户不提供自己的值,它的默认值为 999999999。
-
设置为默认值后,您必须将其设置为空。默认值不显示。
-
我已经调试,单步执行并确认它在传递给 where 子句时不为空。
标签: c# asp.net entity-framework-core