Linq to SQL Select 在 DateTime 上不起作用
本文关键字:DateTime 不起作用 Select to SQL Linq | 更新日期: 2023-09-27 18:37:21
大家好,我的 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,但它也不起作用
看起来您正在尝试获取在某一天注册的所有用户的计数。 如果是这样,我认为您最好使用 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() } );
您需要
做的是在这些日期之间进行选择。
像这样:
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();