C# 如何在 Linq to SQL 中添加日期到日期

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/1227648/
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-06 13:56:16  来源:igfitidea点击:

How to add day to date in Linq to SQL

c#datetimelinq-to-sql

提问by Anthony D

I am writing this code. Here dt is input into the function, as well as someint. The column Exp is a T-SQL date column, which comes as a DateTime through Linq.

我正在编写这段代码。这里 dt 是函数的输入,还有 someint。Exp 列是一个 T-SQL 日期列,它通过 Linq 作为 DateTime 出现。

return (from a in dataContext.TableOfA
       where a.name == "Test" &&
       a.Exp.Value.AddDays(Convert.ToDouble(Someint)) >= new DateTimeOffset(dt)
       select a).First();

In C#, you can add a double as a day to a date time. Meaning you can add 1.5 days. In T-SQL you can only add 1 day, then 12 hours. You must add an int for each part. So when Linq translates AddDays to T-SQL, it converts my number of days to milliseconds, and adds those. This allows it to give all the precision the double gives C#.

在 C# 中,您可以向日期时间添加双精度值作为一天。这意味着您可以添加 1.5 天。在 T-SQL 中,您只能添加 1 天,然后是 12 小时。您必须为每个部分添加一个 int。因此,当 Linq 将 AddDays 转换为 T-SQL 时,它会将我的天数转换为毫秒,然后添加这些天数。这允许它提供 double 给 C# 的所有精度。

Here's the rub. When this gets to SQL, I get the error:

这是摩擦。当这到达 SQL 时,我收到错误消息:

The datepart millisecond is not supported by date function dateadd for data type date

日期函数 dateadd 不支持数据类型 date 的 datepart 毫秒

Basically you can't add milliseconds to a date. Well no kidding. But how do I get something that translates here? I want to add int days to a date. Is the only want to do this to add the negative of them to the other guy I am comparing against? What if I wanted to compare to columns while adding to one?

基本上你不能在日期上添加毫秒。好吧,别开玩笑了。但是我如何得到在这里翻译的东西?我想在日期中添加 int 天。这样做的唯一目的是将它们的负面影响添加到我与之比较的另一个人中吗?如果我想在增加一列的同时与列进行比较怎么办?

Update 1

更新 1

Keith wrote, A command like datepart(millisecond, 10000, myDate) has been supported in T-SQL since at least SQL Server 2000. This error suggests that whatever database you are using does not support the millisecond date part, which seems strange to me.

Keith 写道,至少从 SQL Server 2000 开始,T-SQL 就支持像 datepart(millisecond, 10000, myDate) 这样的命令。此错误表明您使用的任何数据库都不支持毫秒日期部分,这对我来说似乎很奇怪.

Please note I am using SQL Server 2008. It is not supported on the DATE data type. It is supported on datetime.

请注意我使用的是 SQL Server 2008。它不支持 DATE 数据类型。它在日期时间上受支持。

采纳答案by Anthony D

I just changed the column back to a DateTime.

我只是将列改回 DateTime。

回答by Keith

Have you considered writing your own user-defined function that basically takes in an int (for number of days) and date, returns the results of a dateadd()? Then pull that into the LINQ class in your project. Then, instead of calling .AddDate(), call your UDF - this will allow you to keep everything in your LINQ query without having to manually piece together a SQLCommand.

您是否考虑过编写自己的用户定义函数,该函数基本上接受一个 int(表示天数)和日期,并返回 dateadd() 的结果?然后将其拉入项目中的 LINQ 类。然后,不是调用 .AddDate(),而是调用您的 UDF - 这将允许您将所有内容保留在 LINQ 查询中,而无需手动拼凑 SQLCommand。

回答by Ihar Bury

You can also use SqlMethods.DateDiffDaymethod, if you're trying to compare two dates

如果您尝试比较两个日期,您还可以使用SqlMethods.DateDiffDay方法

回答by Dave

If you are using the Entity Framework, use the System.Data.Objects.EntityFunctions as below:

如果您使用的是实体框架,请使用 System.Data.Objects.EntityFunctions,如下所示:

c.CMT_TS > System.Data.Objects.EntityFunctions.AddDays(e.Call.CALL_TS, 1)

回答by tomsv

You can move the call to AddDaysfrom the SQL part to the .NET part:

您可以将调用AddDays从 SQL 部分移动到 .NET 部分:

a.Exp.Value >= new DateTimeOffset(dt).AddDays(-Convert.ToDouble(Someint))

回答by John Zabroski

It seems like this scenario is mostly fixed in .NET 4.5.

似乎这种情况主要在 .NET 4.5 中得到修复。

However, if you write:

但是,如果你写:

var activeProducts = from p in db.Products where DateTime.Now() <= p.EndDate.Date.AddDays(7) select p;

You will get the same error message as described in the OP.

您将收到与 OP 中所述相同的错误消息。

But, if you write:

但是,如果你写:

var activeProducts = from p in db.Products where DateTime.Now() <= p.EndDate.AddDays(7) select p;

Then you can enter the land of milk and honey. Note that I did not call .Date on the database's DateTime representation. This seems to be an edge case missing test coverage by the LINQ 2 SQL framework in 4.5

然后你就可以进入流奶与蜜之地。请注意,我没有在数据库的 DateTime 表示中调用 .Date 。这似乎是 4.5 中 LINQ 2 SQL 框架缺少测试覆盖的边缘情况

回答by kcsekhar13

Create a new DateTimeobject and use AddDaysmethod after that:

创建一个新DateTime对象并AddDays在此之后使用方法:

new DateTime(t.Key.Year,t.Key.Month,T.Key.Day).AddDays(xx)

回答by rana

May be good idea if you would first change your Sql query to Linq and process linq query instead.

如果您首先将 Sql 查询更改为 Linq 并改为处理 linq 查询,这可能是个好主意。

Just add ToList() or limit record

只需添加 ToList() 或限制记录

return (from a in dataContext.TableOfA.ToList()
   where a.name == "Test" &&
   a.Exp.Value.AddDays(Convert.ToDouble(Someint)) >= new DateTimeOffset(dt)
   select a).First();

So once you have all your stuff in Linq then you can process more effectivly I think.

因此,一旦您在 Linq 中拥有所有内容,我认为您可以更有效地进行处理。

Cheers

干杯

回答by PeterMacko

You can use: System.Data.Entity.DbFunctions.AddDays(YourDateTime, NumberOfDays)This function will be translated to valid SQL Query. Class System.Data.Entity.Core.Objects.EntityFunctionswhich also contains AddDays function is obsolete.

您可以使用:System.Data.Entity.DbFunctions.AddDays(YourDateTime, NumberOfDays)此函数将转换为有效的 SQL 查询。类System.Data.Entity.Core.Objects.EntityFunctions也包含AddDays功能已经过时了。

回答by is_oz

I solve my problem with that query.

我用那个查询解决了我的问题。

return (from a in dataContext.TableOfA
       where a.name == "Test" &&
       DbFunctions.AddHours(DbFunctions.CreateDateTime(
 a.Exp.Value.Year,a.Exp.Value.Month,a.Exp.Value.Day,a.Exp.Value.Hour,a.Exp.Value.Minute,
a.Exp.Value.Millisecond),Someint) >=     
DbFunctions.CreateDateTimeOffset(dt.Year,dt.Month,dt.Day,dt.Hour,dt.Minute,dt.Millisecond,0)
       select a).First();