如何在LINQ中添加dateadd? [英] how to make dateadd in LINQ?
问题描述
我有此代码...
其中p.ProductID == product&& (p.PurchaseDate< DateTime.Now.AddDays(-56)&& p.PurchaseDate> DateTime.Now.AddDays(-49))
我执行了LINQ但出现了错误..
我用事件探查器捕获了查询,而where子句看起来像这样..
@ p8 =''2011-02-17 18:43:30.5500000'',@ p9 = 688,@ p10 =''2011-02-17 18:43:30.5530000'',...
这意味着DateTime.Now.AddDays过去了毫秒..
我只需要约会!
我想使用sql中的DateAdd函数.
我该怎么办?
请帮忙....
I have this code...
where p.ProductID == product && ( p.PurchaseDate < DateTime.Now.AddDays(-56) && p.PurchaseDate > DateTime.Now.AddDays(-49))
and I executed the LINQ but I got an error..
I caught the query with profiler and the where clause looked like this..
@p8=''2011-02-17 18:43:30.5500000'',@p9=688,@p10=''2011-02-17 18:43:30.5530000'',...
which means DateTime.Now.AddDays were passing milliseconds ..
I only need date!
I would like to use DateAdd function from sql.
how can I do that?
please help....
推荐答案
调用两次DateTime.Now
,结果会不同毫秒.您必须缓存当前时刻:
CallingDateTime.Now
two times, results to different milliseconds. You have to cache the current moment:
DateTime now = DateTime.Now;
DateTime fromDate = new DateTime( now.Year, now.Month, now.Day ).AddDays( -49 );
DateTime untilDate = new DateTime( now.Year, now.Month, now.Day ).AddDays( -56 );
...
where p.ProductID == product && ( p.PurchaseDate < untilDate && p.PurchaseDate > fromDate )
我不知道您的代码为什么不起作用(除了下面的 ** 1 ),但我相信这不是因为AddDays()
方法,除非您的安装已损坏.
我刚刚进行了一些测试以确保.我正在检查其他两个日期之间的测试日期".
我创建了一个简单的表单,其中包含:
2个NumericUpDown
控件(nudStart和nudEnd)用于定义要检查的日期范围.
1DateTimePicker
(dateTimePicker1)用于定义测试日期"
1Button
(btnGo)启动测试
1TextBox
(txtResult-启用MultiLine,WordWrap启用)以显示结果
这是我使用的代码:
I do not know why your code is not working (Except see **1 below) but I am confident that it is not because of a malfunction in theAddDays()
method, unless your installation has become corrupted.
I have just run some tests to make sure. I am checking for a ''test date'' being between two other dates.
I created a simple Form containing:
2NumericUpDown
controls (nudStart and nudEnd) for defining the range of dates to check.
1DateTimePicker
(dateTimePicker1) for defining the ''test date''
1Button
(btnGo) to fire off the test
1TextBox
(txtResult - MultiLine on, WordWrap on) to display the outcome
Here is the code I used:
public partial class DateTimeTestForm : Form
{
DateTime rangeStart = DateTime.MinValue;
DateTime rangeEnd = DateTime.MaxValue;
public DateTimeTestForm()
{
InitializeComponent();
}
private void ShowResult(bool isValid)
{
string resultString = "The test date {0:D} is{1} between {2:D} and {3:D}";
this.txtResult.Text = string.Format(resultString, this.dateTimePicker1.Value,
isValid ? "" : " NOT",
rangeStart,
rangeEnd);
}
private void btnGo_Click(object sender, EventArgs e)
{
ShowResult(this.dateTimePicker1.Value > rangeStart && this.dateTimePicker1.Value < rangeEnd);
}
private void nudStart_ValueChanged(object sender, EventArgs e)
{
this.rangeStart = DateTime.Now.AddDays((double)this.nudStart.Value);
}
private void nudEnd_ValueChanged(object sender, EventArgs e)
{
this.rangeEnd = DateTime.Now.AddDays((double)this.nudEnd.Value);
}
}
如果您想自己尝试一下,则需要连接事件处理程序,将nudStart设置为-56并将nudEnd设置为-49,在DateTimePicker中选择要测试的日期(我在2011年2月4日和2011年2月8日进行了两次测试)然后单击btnGo.您会看到AddDays()
正确设置了日期.
** 1 我注意到您正在检查PurchaseDate
不在您的日期范围内.这是您想要的吗?
If you want to try this for yourself you will need to hook up the event handlers, set nudStart to -56 and nudEnd to -49, select a date to test in the DateTimePicker (I did two tests 4th Feb 2011 and 8th Feb 2011) then click btnGo. You will see that AddDays()
correctly sets the dates.
**1 I notice that you are checking for the PurchaseDate
being outside of your date range. Is this what you intended?
这篇关于如何在LINQ中添加dateadd?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!