-5

大家好,我的 linq to sql 选择不适用于 dateTime 字段

 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 但它也不起作用

4

2 回答 2

2

看起来您正在尝试计算在某一天注册的所有用户。如果是这样,我认为您最好使用group 关键字按注册日期将所有用户分组在一起。然后,您可以创建一个新的匿名类型以包含日期和计数,如下所示:

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() } );
于 2012-06-16T15:34:15.437 回答
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();
于 2012-06-16T15:20:12.560 回答