【问题标题】:Linq query comparing dates - convert string columnLinq 查询比较日期 - 转换字符串列
【发布时间】:2018-11-02 17:10:03
【问题描述】:

我的 linq 查询有问题。不幸的是,我在数据库中有一个带有日期的列,它是字符串类型。我尝试将日期与 linq 查询进行比较。 不幸的是,我不知道如何将 p.Dates 的值转换为格式 datatime "yyyy-MM-dd"

 int count_month_po = (from p in repozytorium.GetTable<OB_Zap>()
                                       where p.ID_inwest == dgvData.Rows[i].Cells[1].Value.ToString() && (p.Dates > myDate)
                                       select p).Count();

【问题讨论】:

  • 不要将日期存储为字符串,这就是为什么,改变你的数据库类型,让你的生活更轻松
  • 你试过Convert.ToDateTime(p.Dates)吗?
  • @Tim Schmelter 非常好用!!!

标签: c# .net linq


【解决方案1】:

在比较之前使用DateTime.ParseExact()将其解析为DateTime

int count_month_po = (from p in repozytorium.GetTable<OB_Zap>()
                                       where p.ID_inwest == 
dgvData.Rows[i].Cells[1].Value.ToString() && (DateTime.ParseExact(p.Dates,"yyyy-MM-dd", CultureInfo.InvariantCulture) > myDate)
                                       select p).Count();

【讨论】:

猜你喜欢
  • 1970-01-01
  • 2011-12-18
  • 2023-04-04
  • 2010-12-25
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2013-01-19
相关资源
最近更新 更多