2013-02-12 77 views
0

我想从我的表中获取今天的记录。 我写下面的查询 -今天的记录从linq到sql

public ICollection<DashboardNotification> GetNotificationOfToday() 
     { 
      DateTime todaysDate = DateTime.Now; 
      DateTime yesterdaysDate = DateTime.Now.AddDays(-1); 
      db = new BobTheBuilderEntities(); 
      var notificationList = (from n in db.DashboardNotifications.OrderByDescending(n => n.NotificationDateTime) 
            where (n.NotificationDateTime > yesterdaysDate && n.NotificationDateTime <= todaysDate) 
            select n).ToList(); 

      return notificationList; 
     } 

enter image description here

但上面的查询不能正常工作,导致它获取记录从昨天开始了。

如何解决此问题?

+0

是您的病情是否正确? (n.NotificationDateTime> yesterdayDaysDate && n.NotificationDateTime <= todaysDate)。首先你要检查NotificationDateTime应该大于昨天(这意味着今天),然后你检查NotificationDateTime应该是LESS而不是等于今天的日期? – 2013-02-12 05:27:06

+0

@Behroz:最初我只检查今天的日期,但它不工作,然后我GOOGLE了,然后我知道我必须将它与两个日期进行比较,然后只有我会得到结果,这就是为什么我添加了昨天的日期进行比较。 – Priyanka 2013-02-12 05:36:57

回答

0

尝试以下查询

var notificationList = (from n in db.DashboardNotifications.OrderByDescending(n => n.NotificationDateTime) 
            where (n.NotificationDateTime > yesterdaysDate) 
            select n).ToList(); 
+0

它根本不工作,显示linq到实体的错误不支持AddDays(-1)函数..就像那样。 – Priyanka 2013-02-12 06:05:38

+0

ahan。立即尝试编辑的版本并分享结果。 – 2013-02-12 06:15:54

0

你能请尝试宣告昨天的日期作为变量第一

var yesterDay = DateTime.Now.AddDays(-1); 
var notificationList = (from n in db.DashboardNotifications.OrderByDescending(n => n.NotificationDateTime) 
         where (n.NotificationDateTime > yesterDay) 
         select n).ToList(); 
+0

对不起@Behroz,我没有看到你的编辑答案之前发布我的..道歉 – 2013-02-12 06:20:30