使用linq从datetime进行日期比较 [英] date comparison from datetime using linq

查看:87
本文介绍了使用linq从datetime进行日期比较的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

HI,







我想用linq比较日期时间的日期部分,当我尝试它显示转换错误





I want to compare only the date part from datetime using linq,when am trying it's showing conversion error

推荐答案

假设它是一个DateTime值,那么它很简单: DateTime.Date [ ^ ]丢弃时间部分,因此比较仅为日期。



但这不涉及任何转换,因此我怀疑您正在尝试将字符串作为日期和问题进行比较。不要这样做:将你的日期转换为DateTime并使用它们 - 你有可能将数据库中的日期存储为字符串,因此其中一些日期是不兼容的格式,或者只是完全错误。我们无法帮助您 - 您必须首先修复数据库设计,然后转换数据。
Assuming it's a DateTime value, then it's simple: DateTime.Date[^] discards teh time portion, so the comparisons would be date only.

But that doesn't involve any conversions, so I suspect that you are trying to compare strings as dates and getting problems. Don't do that: convert your dates to DateTime and use them - the chances are that you have stored dates in you DB as strings, and as a result some of them are in incompatible formats, or just plain wrong. We can't help you there - you will have to fix your db design first and then convert your data.


这篇关于使用linq从datetime进行日期比较的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆