【问题标题】:Error in comparing DateTime比较日期时间时出错
【发布时间】:2013-06-23 06:31:12
【问题描述】:

我第一次尝试比较DateTime

var searchResult = new PagedData<SAMPLE_STOCK>
{
Data = _db.SAMPLE_STOCK.Where(m => m.LAST_UPDATED.Date == lastUpdate.Date)
};
return PartialView(searcReasult);

它给出了以下错误

The specified type member 'Date' is not supported in LINQ to 
Entities. Only initializers, entity members, and entity navigation 
properties are supported.

然后我写下面的代码

var searchResult = new PagedData<SAMPLE_STOCK>
{
Data = _db.SAMPLE_STOCK.Where(m => m.LAST_UPDATED.Day == lastUpdate.Day 
                                && m.LAST_UPDATED.Month == lastUpdate.Month
                                && m.LAST_UPDATED.Year==lastUpdate.Year)
};

它工作正常。我的问题是....这些有什么区别??

【问题讨论】:

标签: c# asp.net-mvc-3 datetime linq-to-entities


【解决方案1】:

抛出异常是因为您的谓词无法翻译成 SQL。 如果您将其转换为 Enumerable,它可能会起作用,因为您强制使用 linq2object 过滤,但我认为您的第二个查询比这更有效:

var searchResult = new PagedData<SAMPLE_STOCK>
{
    Data = _db.SAMPLE_STOCK.AsEnumrable().Where(m => m.LAST_UPDATED.Date == lastUpdate.Date)
};
return PartialView(searcReasult);

这是你的选择,这个选项提供更少的代码,但更多的内存使用。

【讨论】:

    【解决方案2】:

    消息

    The specified type member 'Date' is not supported in LINQ to Entities. Only initializers, entity members, and entity navigation properties are supported.

    非常不言自明。 SQL 不知道如何获取 DateTime 类型的 Date 属性。 它可以与日期时间一起使用。它可以与整数一起使用。 在你的情况下,你必须让它与整数一起工作。

    就性能而言。我没有看到任何不良影响。

    PS:SQL中Date数据类型的支持与此无关。

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2023-03-08
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2015-07-20
      • 2012-12-29
      • 2012-01-20
      相关资源
      最近更新 更多