如何添加的LINQ当天日期SQL [英] How to add day to date in Linq to SQL

查看:152
本文介绍了如何添加的LINQ当天日期SQL的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我写此代码。这里dt为输入的功能,以及someint。列EXP是一个T-SQL日期列,这是作为通过LINQ的日期时间。

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.

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

在C#中,你可以添加一个兼作一天的日期时间。这意味着你可以添加1.5天。在T-SQL,你只能加1次,然后12小时。您必须为每个零件添加一个int。因此,当转换的LINQ到AddDays T-SQL,将其转换我的天数,以毫秒为单位,并增加了这些。这使得它可以提供所有精密双给C#。

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#.

这里的难题。当此获取到SQL,我得到的错误:

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

日期部分毫秒是日期函数DATEADD为$ B支持的并不
$ b数据类型日期

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

基本上你不能添加毫秒的日期。好了不开玩笑。但是,我怎么得到的东西,在这里翻译?我想INT天添加日期。是唯一的希望这样做对他们的负面添加到其他人,我比较反对?如果我想比较的列,同时增加一个?

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?

更新1

基思写道,像
DATEPART(毫秒,10000,数值指明MyDate)
的命令已经在T-SQL,因为在
最少的SQL Server 2000。这支持了错误
表明,无论数据库您
使用不支持
毫秒的日期部分,这似乎是
怪我。

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.

请注意,我使用SQL Server 2008中,不支持对DATE数据类型。它支持日期时间。

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

推荐答案

我只是改变了列回日期时间。

I just changed the column back to a DateTime.

这篇关于如何添加的LINQ当天日期SQL的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆