Linq比较字符串日期 [英] Linq compare string date
本文介绍了Linq比较字符串日期的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
我想比较日期字符串.我正在尝试使用linq查询,但是我可以将运算符应用于字符串到字符串类型的操作数.
I want to compare string which is date. I am trying following linq query, but I get operators can be applied to operands of type string to string.
var descriptions =
from a in db.Descriptions
where a.ID == 12
&& a.arrivalDate >= "20110101000000"
&& a.arrivalDate <= "20110131000000"
select a;
有什么想法吗?
推荐答案
您不能将DateTime
变量与String
变量进行比较.
You cannot compare a DateTime
variable with a String
variable.
因此,您需要先创建一个DateTime
:
So you need to create a DateTime
first:
String str1 = "20110101000000";
String str2 = "20110131000000";
String format = "yyyyMMddhhmmss";
DateTime d1 = DateTime.ParseExact(str1, format,null);
DateTime d2 = DateTime.ParseExact(str2, format, null);
和LINQ查询:
var descriptions =
from a in db.Descriptions
let arrivalDate = DateTime.ParseExact(a.arrivalDate, format,null)
where a.ID == 12
&& arrivalDate >= date1
&& arrivalDate <= date2
select a;
这篇关于Linq比较字符串日期的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
查看全文