Warning: file_get_contents(/data/phpspider/zhask/data//catemap/4/sql-server-2008/3.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
C# linq到sql select在dateTime上不工作_C#_Sql Server 2008_Linq To Sql_Datetime_Asp.net Mvc 4 - Fatal编程技术网

C# linq到sql select在dateTime上不工作

C# linq到sql select在dateTime上不工作,c#,sql-server-2008,linq-to-sql,datetime,asp.net-mvc-4,C#,Sql Server 2008,Linq To Sql,Datetime,Asp.net Mvc 4,大家好,我的linq到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

大家好,我的linq到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();
            }
for(DateTime i=FromDate;i

我尝试过i.date,但它不太管用

您需要做的是在这些日期之间进行选择

大概是这样的:

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();
var date=新日期时间(i.年、i.月、i.日、0、0);//将日期时间设置为一天的开始
var Count=(从数据库中的d开始)
其中d.RegistrationDate.Value>=日期和d.RegistrationDate.Value
看起来您正在尝试统计某一天注册的所有用户。如果是这样的话,我认为您最好使用,以便按注册日期将所有用户分组。然后,您可以创建一个新的匿名类型,以包含日期和计数,如下所示:

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 Counts=(从数据库中的d开始)
//只筛选FromDate和Now之间注册的用户
其中(d.RegistrationDate.Value>=FromDate
&&d.注册日期<日期时间.现在)
//按注册日期将所有用户分组(假设
//RegistrationDate是一个DateTime?字段)
d组按d.RegistrationDate.Value.Date到日期
//获取包含日期和号码的新匿名类型
//在该日期注册的用户数
选择新的{Date=Date.Key,Count=Date.Count()});