【问题标题】:How to use query to get today's date activities如何使用查询获取今天的日期活动
【发布时间】:2018-11-09 11:28:35
【问题描述】:
  TodayCheckedStatus = (from checkinout in context.Checkinout
                        join status in context.Status on checkinout.CheckType equals status.Statusid
                        where checkinout.Userid == userinfo.Userid
                        where checkinout.CheckTime == DateTime.Today.Date       
                        orderby checkinout.CheckTime descending
                        select new Checkinout
                        {
                         CheckStatus = status.StatusText,
                         CheckTime = checkinout.CheckTime
                         }).ToList()

我用过这个,但由于某种原因它不起作用,它在页面上什么都没有显示

          where checkinout.CheckTime == DateTime.Today.Date 

【问题讨论】:

  • checkinout.CheckTime 是什么类型?它是 DateTime 类型吗?如果是 DateTime 类型,它在 DateTime 的时间部分是否有一些价值?
  • 是的,它是 DateTime 类型,是的,它具有时间值,但我只需要将日期设为今天的日期
  • 这是 LINQ to EF(可能),而不是 SQL。您使用的是哪个版本?在 SQL 中,您将编写 WHERE cast(CheckTime as date) = @date,其中 @date 是类型为 date 的参数,而不是 datetime。不同的 EF 版本虽然调用 SQL 函数的方式不同
  • 顺便说一句,在使用 ORM 时,您不应该使用这样的连接。 ORM 的工作是从实体之间的关系中生成连接。您也不需要 multiple where 语句。结果相当于AND
  • @E.Lahu 你应该这样做where checkinout.CheckTime.Date == DateTime.Today.Date

标签: c# sql-server entity-framework linq


【解决方案1】:

只是为了重申@ikerbera 在回答中的回复,所需的更改是

where checkinout.CheckTime.Date == DateTime.Today.Date  

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2017-08-20
    • 2019-11-15
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2023-03-08
    • 1970-01-01
    相关资源
    最近更新 更多