【问题标题】:How to compare string with DateTime in Entity Framework如何在实体框架中将字符串与日期时间进行比较
【发布时间】:2014-11-01 12:10:18
【问题描述】:

我有一个实体框架架构,如下所示。

Class Contact
{ 
   string name;
   DateTime creationDate;
}
and i've a string that is

string date="9/9/14 3:00:00 PM"

我正在执行以下查询

var q=from u in db.Contacts where u.creationDate.ToString==date select u;

但我面临以下错误

LINQ to Entities 无法识别方法“System.String ToString()”方法,并且该方法无法转换为存储表达式。

谁能告诉我如何比较 dateTime 和 string ?什么是可能的演员?

【问题讨论】:

  • 将您的字符串解析为 DateTime 对象并在您的查询中使用它。
  • 是的,我通过这个语句 DateTime temp=Convert.ToDateTime(date);但是现在查询没有返回任何结果。

标签: c# .net database entity-framework-4


【解决方案1】:

Change your string to a DateTime 之类的东西(new DateTime() 也不会工作,原因与 .ToString() 不工作的原因相同,即实体框架不知道):

[EdmFunctionAttribute("Edm", "CreateDateTime")]
public static Nullable<DateTime> CreateDateTime(
    Nullable<int> year,
    Nullable<int> month,
    Nullable<int> day,
    Nullable<int> hour,
    Nullable<int> minute,
    Nullable<double> second
)

参数

年 类型:System.Nullable

The year part of the new date.

月 类型:System.Nullable

The month part of the new date.

天 类型:System.Nullable

The day part of the new date.

小时 类型:System.Nullable

The hour part of the new date.

分钟 类型:System.Nullable

The minutes part of the new date.

秒 类型:System.Nullable

The seconds part of the new date. Note that you can specify fractions of a second with this parameter.

【讨论】:

    【解决方案2】:

    类似于以下内容:

    var date = DateTime.Parse("9/9/14 3:00:00 PM");
    
    var q from u in db.Contacts
          where u.creationDate == date
          select u;
    

    【讨论】:

    • 尽管数据库中存在“9/9/14 3:00:00 PM”,但它不返回任何结果
    • 日期解析是否正确?您可以尝试使用 ParseExact 来确定:DateTime.ParseExact("9/9/14 3:00:00 PM", "M/d/yy h:00:ss tt", System.Globalization.CultureInfo.InvariantCulture)
    • 当我解析你的方法时,我遇到了以下错误“字符串未被识别为有效的日期时间。”
    • 你的日期字符串确定是“9/9/14 3:00:00 PM”吗?运行我之前评论中发布的确切代码对我来说很好。
    • 是的。解析现在很好,但比较失败。我没有得到任何数据
    【解决方案3】:
    List<Contact>  t = new List<Contact>();
    
    String date="9/9/14 3:00:00 PM"
    
    var result = t.Where(m => m.creationDate == DateTime.ParseExact(date,"M/d/yy h:mm:ss tt",null));
    

    【讨论】:

    • 字符串未被识别为有效的日期时间。发生错误
    【解决方案4】:

    尝试将日期分成几部分。

     string dt = "9/9/14 3:00:00 PM";
     var date =  DateTime.ParseExact(dt,"M/d/yy h:mm:ss tt",null);
    
     var q from u in db.Contacts
          .Where (u.creationDate.DateTime.Year <= date.Year
                       && u.creationDate.DateTime.Month <= date.Month
                       && u.creationDate.DateTime.Day <= date.Day
                        && u.creationDate.DateTime.Day <= date.Hour
                        && u.creationDate.DateTime.Day <= date.Minute
                        && u.creationDate.DateTime.Day <= date.Second
                       )
            select u;
    

    【讨论】:

      【解决方案5】:

      你可以使用

      var result = t.Where(m => m.creationDate == DateTime.ParseExact(date,"yyyy/MM/dd HH:mm:ss tt",null));
      

      sql server中存储的日期为yyyy-MM-dd格式。

      【讨论】:

        猜你喜欢
        • 1970-01-01
        • 1970-01-01
        • 2018-04-18
        • 2012-05-16
        • 1970-01-01
        • 2017-10-23
        • 2022-11-21
        • 2010-09-05
        • 1970-01-01
        相关资源
        最近更新 更多