Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/csharp/306.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
C# 如何在实体框架中比较字符串和日期时间_C#_.net_Database_Entity Framework 4 - Fatal编程技术网

C# 如何在实体框架中比较字符串和日期时间

C# 如何在实体框架中比较字符串和日期时间,c#,.net,database,entity-framework-4,C#,.net,Database,Entity Framework 4,我有一个实体框架模式,如下所示 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无法识

我有一个实体框架模式,如下所示

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吗?可能的铸造方法是什么?

类似于以下内容:

var date = DateTime.Parse("9/9/14 3:00:00 PM");

var q from u in db.Contacts
      where u.creationDate == date
      select u;
类似于(new DateTime()也不起作用,原因与.ToString()不起作用相同,即实体框架不知道它):

月 类型:System.Nullable

The year part of the new date.
The month part of the new date.
The day part of the new date.
The hour part of the new date.
The minutes part of the new date.
The seconds part of the new date. Note that you can specify fractions of a second with this parameter.
一天 类型:System.Nullable

The year part of the new date.
The month part of the new date.
The day part of the new date.
The hour part of the new date.
The minutes part of the new date.
The seconds part of the new date. Note that you can specify fractions of a second with this parameter.
时辰 类型:System.Nullable

The year part of the new date.
The month part of the new date.
The day part of the new date.
The hour part of the new date.
The minutes part of the new date.
The seconds part of the new date. Note that you can specify fractions of a second with this parameter.
分钟 类型:System.Nullable

The year part of the new date.
The month part of the new date.
The day part of the new date.
The hour part of the new date.
The minutes part of the new date.
The seconds part of the new date. Note that you can specify fractions of a second with this parameter.
第二 类型:System.Nullable

The year part of the new date.
The month part of the new date.
The day part of the new date.
The hour part of the new date.
The minutes part of the new date.
The seconds part of the new date. Note that you can specify fractions of a second with this parameter.
List t=newlist();
字符串日期=“9/9/14 3:00:00 PM”
var result=t.Where(m=>m.creationDate==DateTime.ParseExact(日期,“m/d/yy h:mm:ss tt”,null));

尝试将日期拆分为多个部分

 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;
string dt=“9/9/14 3:00:00 PM”;
var date=DateTime.ParseExact(dt,“M/d/yy h:mm:ss tt”,空);
从u到db触点的var q
.Where(u.creationDate.DateTime.Year您可以使用

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

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

将字符串解析为DateTime对象,并在查询中使用它。是的,我通过以下语句执行了操作DateTime temp=Convert.ToDateTime(日期);但现在查询不返回任何结果。虽然“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”吗?运行我以前评论中发布的确切代码对我来说很好。是的。现在解析很好,但比较失败。我没有得到任何数据字符串未被识别为有效的日期时间。发生错误