比较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(" 8-sep-2017"到" 09/08/2017"one_answers" 9-SEP-2017"至" 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)
    {
        ...
    }
}

最新更新