2013-02-12 82 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