[英]NullReferenceException with Nullable DateTime despite null check
GetTodayItemCount()
attempts to get today's item count using CreatedDtt
in the Items
model. GetTodayItemCount()
尝试使用Items
模型中的CreatedDtt
获取今天的项目计数。 Because CreatedDtt
is a Nullable Datetime ( DateTime?
), I use a ternary operator within the Where
's lambda expression to make sure I am not trying to access the date of a null value later on in my equality comparison. 因为CreatedDtt
是一个Nullable Datetime( DateTime?
),所以我在Where
的lambda表达式中使用了一个三元运算符,以确保我不会在我的相等比较中尝试访问null值的日期。
However, I still get the classic NullReferenceException? Object reference not set to an instance of an object
但是,我仍然得到经典的NullReferenceException? Object reference not set to an instance of an object
NullReferenceException? Object reference not set to an instance of an object
error, and I can confirm that row.CreatedDtt.Value.Date
is where the issue is at. NullReferenceException? Object reference not set to an instance of an object
错误NullReferenceException? Object reference not set to an instance of an object
,我可以确认row.CreatedDtt.Value.Date
是问题所在的位置。
public Int64 GetTodayItemCount()
{
OrmLiteConnectionFactory dbFactory = new OrmLiteConnectionFactory(dbConn, SqlServerDialect.Provider);
using (IDbConnection db = dbFactory.Open())
{
SqlExpression<Items> itemMatch = db.From<Items>()
.Where(row =>
(row.CreatedDtt.HasValue ?
row.CreatedDtt.Value.Date : DateTime.MinValue) == DateTime.Today
);
Int64 itemCount = db.Count(itemMatch);
db.Close();
return itemCount;
}
}
How is row.CreatedDtt.Value.Date
being accessed when I am checking for its valid value beforehand, and how can I accomplish the desired outcome of GetTodayItemCount()
without getting a NullReferenceException
error? 当我事先检查其有效值时如何访问row.CreatedDtt.Value.Date
,如何在不获取NullReferenceException
错误的情况下完成GetTodayItemCount()
的所需结果?
Since the solution doesn't seem to be as straightforward as I had predicted, I am adding the stack trace below in case there is any use in it: 由于解决方案似乎并不像我预测的那样简单,我在下面添加堆栈跟踪以防万一在其中有任何用处:
at ServiceStack.OrmLite.SqlExpression`1.GetQuotedColumnName(ModelDefinition tableDef, String memberName)
at ServiceStack.OrmLite.SqlExpression`1.GetMemberExpression(MemberExpression m)
at ServiceStack.OrmLite.SqlExpression`1.VisitMemberAccess(MemberExpression m)
at ServiceStack.OrmLite.SqlExpression`1.Visit(Expression exp)
at ServiceStack.OrmLite.SqlExpression`1.VisitBinary(BinaryExpression b)
at ServiceStack.OrmLite.SqlExpression`1.Visit(Expression exp)
at ServiceStack.OrmLite.SqlExpression`1.VisitBinary(BinaryExpression b)
at ServiceStack.OrmLite.SqlExpression`1.Visit(Expression exp)
at ServiceStack.OrmLite.SqlExpression`1.VisitLambda(LambdaExpression lambda)
at ServiceStack.OrmLite.SqlExpression`1.Visit(Expression exp)
at ServiceStack.OrmLite.SqlExpression`1.AppendToWhere(String condition, Expression predicate)
at ServiceStack.OrmLite.SqlExpression`1.Where(Expression`1 predicate)
at GroupRedemptionsScanner.DBHandlers.GetTodayItemCount() in [directory/file/line number etc]
You can't apply C# logic to RDBMS Server columns. 您不能将C#逻辑应用于RDBMS服务器列。 If you want to test fuzzy precision data types like Dates you should use a range instead, eg: 如果要测试模糊精度数据类型(如日期),则应使用范围,例如:
.Where(row => row.CreatedDtt != null &&
row.CreatedDtt >= DateTime.Now.Date &&
row.CreatedDtt < DateTime.Now.Date.AddDays(1)));
I tried to reproduce your error in every way you could imagine but I couldn't. 我试图以你能想象的各种方式重现你的错误,但我做不到。 The only way was explicitly leaving a row in null. 唯一的方法是将行显式保留为null。 But I don't know if it's a possible case. 但我不知道这是不是一个可能的情况。 Anyway, I propose you to refactor it using Null Propagation. 无论如何,我建议你使用Null Propagation重构它。 This way: 这条路:
.Where(row => row?.CreatedDtt?.Date == DateTime.Today);
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.