C# 根据日、周和月筛选数据

C# 根据日、周和月筛选数据,c#,linq,entity-framework,C#,Linq,Entity Framework,我想过滤今天、每周和每月的数据 var data = new DashboardViewModel() { Today = new State() { Download = db.Download.Where(x => x.Time == DateTime.Today).Count(), Visit = db.Visit.Where(x => x.Time == DateTime.Today).Count() },

我想过滤今天、每周和每月的数据

var data = new DashboardViewModel()
           {
               Today = new State() { Download = db.Download.Where(x => x.Time == DateTime.Today).Count(), Visit = db.Visit.Where(x => x.Time == DateTime.Today).Count() },
               Week = new State() { Download = db.Download.Where(x => x.Time.DayOfYear <= Day && x.Time.DayOfYear >= Day - 7).Count(), Visit = db.Visit.Where(x => x.Time.DayOfYear <= Day && x.Time.DayOfYear >= Day - 7).Count() },
               Month = new State() { Download = db.Download.Where(x => x.Time.DayOfYear <= Day && x.Time.DayOfYear >= Day - 30).Count(), Visit = db.Visit.Where(x => x.Time.DayOfYear <= Day && x.Time.DayOfYear >= Day - 30).Count() },
               Total = new State() { Download=db.Download.Count(),Visit=db.Visit.Count()}
           };
var data=新的仪表板视图模型()
{
Today=new State(){Download=db.Download.Where(x=>x.Time==DateTime.Today).Count(),Visit=db.Visit.Where(x=>x.Time==DateTime.Today).Count(),
Week=new State(){Download=db.Download.Where(x=>x.Time.DayOfYear=Day-7).Count(),Visit=db.Visit.Where(x=>x.Time.DayOfYear=Day-7),
Month=new State(){Download=db.Download.Where(x=>x.Time.DayOfYear=Day-30).Count(),Visit=db.Visit.Where(x=>x.Time.DayOfYear=Day-30),
总计=新状态(){Download=db.Download.Count(),Visit=db.Visit.Count()}
};
它给出了错误:

LINQ to实体中不支持指定的类型成员“DayOfYear”


如何解决此错误,或者是否有其他更好的方法来解决此问题?

问题是,当LINQ to Entities解析lambda表达式并将其转换为SQL时,它不知道什么是“DayOfYear”,以及它如何映射到有效的SQL。这就是(在v6之前的EF中)的用武之地,它允许您在LINQ中使用日期操纵函数来处理实体。例如,要按周筛选,可以使用:

Week = new State() { 
    Download = db.Download.Where(x => DbFunctions.DiffDays(DbFunctions.TruncateTime(x.Time), Day) <= 7).Count(), 
    Visit = db.Visit.Where(x => DbFunctions.DiffDays(DbFunctions.TruncateTime(x.Time), Day) <= 7).Count() }
Week=newstate(){
Download=db.Download.Where(x=>DbFunctions.DiffDays(DbFunctions.TruncateTime(x.Time),Day)DbFunctions.DiffDays(DbFunctions.TruncateTime(x.Time),Day)请参见:

var ruleDate = Convert.ToDateTime(rule.data).Date;
return jobdescriptions.Where(j.Deadline.Year == ruleDate.Year 
                       && j j.Deadline.Month == ruleDate.Month 
                       && j.Deadline.Day == ruleDate.Day);