C# 在 LINQ to Entities 中使用 DateTime

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/16736252/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-10 01:47:47  来源:igfitidea点击:

Using DateTime in LINQ to Entities

c#datetimelinq-to-entities

提问by max

I have a PostgreSQL database that interacts with the program through Entity Framework Code First.

我有一个 PostgreSQL 数据库,它通过 Entity Framework Code First 与程序交互。

Database contains a table "users" that has column "visit" type of DateTime.

数据库包含一个表“users”,其列“visit”类型为DateTime。

The application is described as;

该应用程序被描述为;

public class Users
{    ...
    [Required]
    [Column("visit")]
    public DateTime VisitDate
    ...
}

I trying to run this query;

我试图运行这个查询;

var rslt = context.Visitors.Where(v => v.VisitDate.Date == DateTime.Now.Date).ToList()

But getting an exception: NotSupportedException

但得到一个例外: NotSupportedException

What's wrong?

怎么了?

采纳答案by MarcinJuraszek

DateTime.Dateproperty is not supported. You have to use SqlFunctions.DatePartmethod instead. It will end up with DATEPARTTSQLmethod within generated SQL query.

DateTime.Date不支持属性。你必须改用SqlFunctions.DatePart方法。它将以DATEPARTTSQL生成的 SQL 查询中的方法结束。

var rslt = context.Visitors
                  .Where(v => SqlFunctions.DatePart("year", v.VisitDate) == SqlFunctions.DatePart("year", DateTime.Now))
                  .Where(v => SqlFunctions.DatePart("dayofyear", v.VisitDate) == SqlFunctions.DatePart("dayofyear", DateTime.Now))
                  .ToList(); 

回答by Erik Schierboom

The problem is that the LINQ provider is trying to convert DateTime.Now.Dateto a database method, which it cannot do by default. The trick to doing date comparison is to create a DateTimeinstance that has its time component set to the default value. You can get more information hereand here.

问题在于 LINQ 提供程序正在尝试转换DateTime.Now.Date为数据库方法,默认情况下它无法执行此操作。进行日期比较的技巧是创建一个DateTime实例,将其时间组件设置为默认值。您可以在此处此处获得更多信息。

回答by Mayur Borad

Use the class EntityFunction for trimming the time portion.

使用 EntityFunction 类来修剪时间部分。

using System.Data.Objects;    

var bla = (from log in context.Contacts
           where EntityFunctions.TruncateTime(log.ModifiedDate) ==  EntityFunctions.TruncateTime(today.Date)
           select log).FirstOrDefault();

Source: http://social.msdn.microsoft.com/Forums/en-US/csharpgeneral/thread/84d4e18b-7545-419b-9826-53ff1a0e2a62/

来源:http: //social.msdn.microsoft.com/Forums/en-US/csharpgeneral/thread/84d4e18b-7545-419b-9826-53ff1a0e2a62/

回答by Tim S

Mayur Borad's answer (IMHO more correct than the accepted answer) has become out of date:

Mayur Borad 的答案(恕我直言,比公认的答案更正确)已经过时了:

System.Data.Entity.Core.Objects.EntityFunctionsis obsolete. You should use System.Data.Entity.DbFunctionsinstead.

System.Data.Entity.Core.Objects.EntityFunctions已过时。你应该System.Data.Entity.DbFunctions改用。

var today = DateTime.Today; // (Time already removed)

var bla = context.Contacts
    .FirstOrDefault(x => DbFunctions.TruncateTime(x.ModifiedDate) == today);