Linq 语句返回 null
我有以下语句
EmployeeLog objEmpployeeLog =
lstEmployeeLog.Where(x => x.EmpId == iEmpId
&& x.InDateTime.Value == lastCheckInDate
&& x.OutDateTime == null).FirstOrDefault();
lstEmpAttnLog 是一个列表,我知道它包含 EmpId、InDateTime 等于传递的参数且 OutDateTime 为空的对象。我使用断点看到了这些值。
我很惊讶为什么它不返回值。
请帮忙,我一无所知,请猜猜可能出了什么问题。
I have following statement
EmployeeLog objEmpployeeLog =
lstEmployeeLog.Where(x => x.EmpId == iEmpId
&& x.InDateTime.Value == lastCheckInDate
&& x.OutDateTime == null).FirstOrDefault();
lstEmpAttnLog is a List, i know it contains the object which has EmpId, InDateTime equal to the argument passed and OutDateTime is null. I saw these values using breakpoint.
I am surprised why it doesn't return the value.
Please help, i am clueless, please guess what could might have gone wrong.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(3)
我的猜测是
x.InDateTime.Value
和lastCheckInDate
之间的差异很小(秒、毫秒等)。尝试将它们四舍五入到某个预先确定的精度。My guess would be small difference (seconds, millis,etc) between
x.InDateTime.Value
andlastCheckInDate
. Try rounding them off to some pre-determined precision.如果没有找到这样的元素,
FirstOrDefault
返回null
。因此,您的查询不匹配任何项目。
正如 Marc 指出的那样,也许您的日期在秒或毫秒上不匹配。
您可以修改查询以查看特定的精度:
请不要使用 Systems Hungarian 在 C# 代码中!这绝对没有意义,因为 C# 具有强大的类型系统,而 Visual Studio 是一个高级 IDE。
FirstOrDefault
returnsnull
if no such element is found.Therefore, you query doesn't match any item.
As Marc pointed out, perhaps your dates don't match in seconds or milliseconds.
You can modify query to look with specific precision:
And please, don't use Systems Hungarian in C# code! It's absolutely pointless, seeing that C# has strong type system and Visual Studio is an advanced IDE.
我假设您正在为 OutDateTime 使用可为 null 的 DateTime 值,如果您将
x.OUTDateTime == null 更改为 !x.OutDateTime.HasValue
I am assuming you are using a nullable DateTime value for OutDateTime, if you are change
x.OUtDateTime == null to !x.OutDateTime.HasValue