【问题标题】:How to search date using nullable datetime?如何使用可为空的日期时间搜索日期?
【发布时间】:2013-12-22 08:43:47
【问题描述】:

我有以下方法:

    [HttpGet]
    public IEnumerable<database_WICs> GetDate(DateTime? start, DateTime? end)
    {
        var data = from c in db.database_WICs
                   where c.UploadDate.Value.Date <= start &&
                   c.UploadDate.Value.Date >= end
                   select c;
        return data.ToList();
    }

我目前遇到“LINQ to Entities 不支持指定类型成员 'Date'”异常错误,使用以下查询字符串:api/data?start=25/11/2013&end=28/11/2013

我是新手编程学生,所以请原谅我的知识和愚蠢的错误。

谢谢

【问题讨论】:

  • 你检查过这个link吗?和this 一个?
  • 感谢您的快速响应和有用的链接。我会再试一次。谢谢

标签: entity-framework asp.net-web-api


【解决方案1】:

使用TruncateTime方法如下:

[HttpGet]
public IEnumerable<database_WICs> GetDate(DateTime? start, DateTime? end)
{
    DateTime startDate = Convert.ToDateTime(start).Date;
    DateTime endDate = Convert.ToDateTime(end).Date;
    var data = from c in db.database_WICs
               Where(c => EntityFunctions.TruncateTime(c.UploadDate) <= startDate
                 &&  c => EntityFunctions.TruncateTime(c.UploadDate) >= endDate)
               Select c;
    return data.ToList();
}

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 2023-03-26
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多