2012-06-16 84 views
-5

大家好我的LINQ to SQL的選擇不會對日期時間字段工作LINQ to SQL的選擇不工作的日期時間

for (DateTime i = FromDate; i < DateTime.Now; i = i.AddDays(1)) 
      { 
       var Count = (from d in db.Users 
          where d.RegistrationDate.Value == i 
          select d.ID).Count(); 
      } 

我已經嘗試i.date,但它不工作太

+0

consieder to use by by by – 2kay

回答

2

它看起來像你試圖讓誰在某一天的所有註冊用戶的數量。如果是這樣,我認爲你最好使用group keyword按註冊日期將所有用戶分組在一起。然後你可以創建一個新的匿名類型來包含日期和計數,如下所示:

var Counts = (from d in db.Users 
      // filter down to only users registered between FromDate and Now 
      where (d.RegistrationDate.Value >= FromDate 
       && d.RegistrationDate < DateTime.Now) 
      // Group all users together by Date of registration (assuming 
      // RegistrationDate is a DateTime? field) 
      group d by d.RegistrationDate.Value.Date into date 
      // Get a new anonymous type that contains the date and the number 
      // of users registered on that date 
      select new { Date = date.Key, Count = date.Count() }); 
1

你什麼需要做的是在這些日期之間做一個選擇。

事情是這樣的:

var date = new DateTime(i.Year, i.Month, i.Day, 0, 0, 0); // Set the datetime to start of the day 
var Count = (from d in db.Users 
          where d.RegistrationDate.Value >= date && d.RegistrationDate.Value < date.AddDay(1) 
          select d.ID).Count();