问题描述
如果我有这样的Linq
到SQL
表达式:
If I have a Linq
to SQL
expression like this:
from subscription in dbContext.Subscriptions
where subscription.Expires > DateTime.Now
select subscription
我希望它使用SQL Servers GETDATE()
函数,而不是计算机运行C#
程序的时间.
I want this to to use the SQL Servers GETDATE()
function instead of the time of the machine running the C#
program.
下一个问题是如何翻译:
The next question would be how to translate this:
DateTime.Now.AddDays(2)
对此:
DATEADD(dd, 2, GETDATE())
推荐答案
尝试此:
[Function(Name="GetDate", IsComposable=true)]
public DateTime GetSystemDate()
{
MethodInfo mi = MethodBase.GetCurrentMethod() as MethodInfo;
return (DateTime)this.ExecuteMethodCall(this, mi, new object[]{}).ReturnValue;
}
编辑:这需要成为您的DataContext类的一部分.
EDIT: this needs to be a part of your DataContext class.
现在,您可以在查询中使用GetSystemDate()代替DateTime.Now.至于日期差异,请查看System.Data.Linq.SqlClient命名空间,尤其是SqlMethods类的DayDiffXXX函数.
Now you can use GetSystemDate() instead of DateTime.Now in your queries.As for date differences take a look at System.Data.Linq.SqlClient namespace, especially DayDiffXXX functions of SqlMethods class.
这篇关于如何在Linq to SQL表达式中使用SQL的GETDATE()和DATEADD()?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!