林克统计每周咨询的次数,从过去的28周到本周

本文关键字:过去 28周 每周 统计 林克 | 更新日期: 2023-09-27 18:29:12

我有一个名为Consult的sql表,它代表物理治疗师的咨询(治疗)

ConsultID ConsultDate Therapist Location

我想每周汇总这些数据,从28周前开始,直到本周

类似:

Location     Week-1          Week-2          Week-3 .....      Week-28
Amsterdam    41              38              34                55
Utrecht      65              56              46                46

我怎么能在林克做到这一点?我有以下内容:

public ActionResult Therapist(int? id)
    {
        if (id == null)
        {
            return new HttpStatusCodeResult(HttpStatusCode.BadRequest);
        }
        DateTime startDate = DateTime.Now.StartOfWeek(DayOfWeek.Monday).AddDays(-168);
        DateTime endDate = DateTime.Now.StartOfWeek(DayOfWeek.Sunday);
        var TherapistConsult = from row in db.Consults
                               where ((row.Therapist == id) && (row.ConsultDate > startDate) && (row.ConsultDate < endDate))
                                 group row by row.Location into g
                                 where g.FirstOrDefault() != null
                                 select new
                                 {
                                     Location = g.Key,
                                     // Need a loop here for 28 weeks in the past till this week
                                     // WeekN = g.Count(x => x.Week == N),
                                 };
        return View(TherapistConsult.ToList());
    }

林克统计每周咨询的次数,从过去的28周到本周

您想要的是"数据透视"您的数据,从SQL比从linq更容易做到这一点,
在CodeProject中查找此
http://www.codeproject.com/Tips/500811/Simple-Way-To-Use-Pivot-In-SQL-Query
如果由于某种原因必须在linq中执行此操作,请查找:
使用LINQ 的数据透视