2012-12-06 63 views
0

如何比较Linq中的小时?Linq比较小时

我使用C#4.0和SQL Server 2008 R2。 我的数据:

CLIENT_ID (varchar) and HORRAIRE (time 7) 

和我的代码:

IList<LS_CLIENTHORRAIRE> LesListe; 
       using (Soft8Exp_ClientEntities oEntite_T = new Soft8Exp_ClientEntities()) 
       { 
        var query = from o in oEntite_T.LS_CLIENTHORRAIRE where o.CLIENT_ID == CLIENT_ID && o.HORRAIRE >= [the Computer Time] select o; 
        LesListe = query.ToList(); 
       } 
       return LesListe; 

回答

1

time数据类型在SQL maps toTimeSpan在.NET中,目前要比较它针对当前系统时间,你可以使用:

DateTime.Now.TimeOfDay 

所以你的查询会是:

var query = from o in oEntite_T.LS_CLIENTHORRAIRE 
      where o.CLIENT_ID == CLIENT_ID && o.HORRAIRE >= DateTime.Now.TimeOfDay 
      select o; 

参见:DateTime.TimeOfDay

+0

感谢你,但上的TimeOfDay LINQ – user609511

1
o.HORRAIRE >= new TimeSpan(00/*hours*/, 00/*minutes*/, 00/*seconds*/) 
0
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; 
+0

无法正常工作,您应该添加一些文字到你的答案 – tuergeist