[英]Linq select time with date + 1
In my SQL 2008 R2, I have this table: 在我的SQL 2008 R2中,有以下表格:
I have create the LINQ syntax to select all hours that greater than computer hour: 我创建了LINQ语法来选择大于计算机时间的所有时间:
public IList<LS_CLIENTHORRAIRE> Get_All_Obj(string CLIENT_ID)
{
try
{
IList<LS_CLIENTHORRAIRE> LesListe;
using (Soft8Exp_ClientEntities oEntite_T = new Soft8Exp_ClientEntities())
{
int The_Hour = DateTime.Now.Hour;
var query = from o in oEntite_T.LS_CLIENTHORRAIRE where o.CLIENT_ID == CLIENT_ID && o.HORRAIRE > new TimeSpan(The_Hour, 00/*minutes*/, 00/*seconds*/) select o;
LesListe = query.ToList();
}
return LesListe;
}
catch (Exception excThrown)
{
throw new Exception("Err_02", excThrown);
}
}
and it is work fine: 它工作正常:
08:00
then it return 12:00
and 19:00
, 08:00
19:00
,它将返回12:00
和19:00
, 13:00
then it return 19:00
. 13:00
时,它将返回19:00
。 But now I want to change the program that look at the date also: 但是现在我也想更改看日期的程序:
08:00
then it return 12:00
and 19:00
and 05:00
. 08:00
时,它将返回12:00
和19:00
和05:00
。 13:00
then it return 19:00
and 05:00
. 13:00
时,它将返回19:00
和05:00
。 20:00
then it return 05:00
. 20:00
时,它将返回05:00
。 Any idea ? 任何想法 ?
i have tried like this: 我已经试过像这样:
DateTime The_Date = DateTime.Now.Date;
var query = from o in oEntite_T.LS_CLIENTHORRAIRE where o.CLIENT_ID == CLIENT_ID && The_Date.Add(o.HORRAIRE) > DateTime.Now select o;
LesListe = query.ToList(
); );
but unfortunately it not work 但不幸的是它不起作用
var query = oEntite_T.LS_CLIENTHORRAIRE
.Where(o => o.CLIENT_ID == CLIENT_ID &&
(o.HORRAIRE > DateTime.Now.TimeOfDay ||
o.HORRAIRE.Hours == 5));
That would make the method behave like you'd like. 这将使该方法的行为像您想要的那样。 Though I don't understand, why you want 5:00 returned, even though it is actually smaller than
DateTime.Now = 13:00
. 尽管我不明白,但为什么要返回5:00,尽管实际上它小于
DateTime.Now = 13:00
。
I think your table lacks date information, which makes the logic broken. 我认为您的表缺少日期信息,这使逻辑混乱。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.