比较数据库中的日期. [英] Compare date in database.
本文介绍了比较数据库中的日期.的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
var _tol = from to in ddc.tbl_LeadAssigns
where to.ToDate == DateTime.Today && to.UserId == new Guid(sID)
select to;
foreach (var cnt in _tol)
{
todayCount = todayCount + Convert.ToInt32(cnt.TotalLead);
}
lblToday.Text = todayCount.ToString();
推荐答案
问题可能是您从数据库检索的DateTime值上有一个时间,因此比较将永远不会成功.
试试:
The problem is probably that the DateTime value you retrieve from the database will have a time on it, so the comparison will never succeed.
Try:
where to.ToDate.Today == DateTime.Today && to.UserId == new Guid(sID)
那可能会解决它.如果没有,那么我们将需要更多信息.
That may fix it. If it doesn''t then we will need a bit more info.
我将基于两个假设回答您的问题.
1.如果您的表包含ToDate
列的不可为空的值.如果是,则必须使用ToDate.Value
2.如果仅关注ToDate
列的Month,Day and Year
值.不是hour, minute and second
值.如果是这样,您可以根据这些值进行格式化.
如果我们对此表示同意,则以下代码将从linq查询中正确选择日期值
I will answer your question based on two assumption.
1.If your table contains non nullable value ofToDate
column.If so you have to useToDate.Value
2.If you concerned only aboutMonth,Day and Year
values of theToDate
column. Nothour, minute and second
values. If so you can format based on these values.
If we agree on this, the following code will properly select date value from your linq query
var query = from to in ddc.tbl_LeadAssigns
select new { formatedDate = to.ToDate.Month + "/" + to.ToDate.Day + "/" + to.ToDate.Year, to.UserId };
var _tol = from to in query
where to.formatedDate.Equals(DateTime.Now.ToShortDateString())
select to;
foreach (var cnt in _tol)
{
todayCount = todayCount + Convert.ToInt32(cnt.TotalLead);
}
lblToday.Text = todayCount.ToString();
同样,我离开了很多假设,例如文化等等.
Again there are a lot of assumption that I left, like culture and more.
如果您在数据库中保存了较长的日期时间,则只能检查以下代码一天(
If you save long date time in your database ,you can check the following code for only one day
var _tol = "from to in ddc.tbl_LeadAssigns
where to.ToDate > "+ DateTime.Today + " &&"+ "to.ToDate < "+ DateTime.Today.AddDays(1) +" <pre lang="sql">&& to.UserId == new Guid(sID)
select to;
foreach (var cnt in _tol)
{
todayCount = todayCount + Convert.ToInt32(cnt.TotalLead);
}
lblToday.Text = todayCount.ToString();
最好的问候,
Theingi Win
Best Regard ,
Theingi Win
这篇关于比较数据库中的日期.的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
查看全文