繁体   English   中英

在LINQ C#中比较两个日期(数据是字符串类型)的最佳方法

[英]Best way to compare two date(data is string type) in linq c#

我想比较下面的代码在LINQ C#中的两个日期(数据是字符串类型)。

var checkStoreClose = (from s in database.STORE
    where s.StoreClose_DT < txt_inputDateFromUser
    select s);
// s.StoreClose_DT is string "08-Sep-2017"
// txt_inputDateFromUser is string "09-Sep-2017"

我想我应该做的步骤如下:

  1. 将字符串从dd-MM-yyyy转换为字符串MM / dd / yyyy(“ 2017年9月8日”到“ 09/08/2017”,从“ 2017年9月9日”到“ 09/09/2017”)
  2. 将字符串从MM / dd / yyyy转换为日期(从“ 09/08/2017”到09/08/2017,从“ 09/09/2017”到09/09/2017)
  3. 我可以比较日期。

你能建议我吗?

在这里,日期转换发生在客户端。

var inputDate = DateTime.Parse(txt_inputDateFromUser);
var checkStoreClose = (from s in database.STORE
    where DateTime.Parse(s.StoreClose_DT) < inputDate
    select s);

你可以这样做:

var checkStoreClose = (from s in database.STORE
where DateTime.Parse(s.StoreClose_DT) < DateTime.Parse(txt_inputDateFromUser)
select s);
DateTime dtBusinessDate;
DateTime dtStoreClosedDate;
if (DateTime.TryParseExact(txt_inputDateFromUser.Value, "d-MMM-yyyy", CultureInfo.InvariantCulture, DateTimeStyles.None, out dtBusinessDate) && DateTime.TryParseExact(getStoreCloseDT, "d-MMM-yyyy", CultureInfo.InvariantCulture, DateTimeStyles.None, out dtStoreClosedDate))
{
    if (dtBusinessDate > dtStoreClosedDate)
    {
        ...
    }
}

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM