2016-08-18 13 views
0

林有一個問題,我不能拿過去,這裏是我的控制器報告 -獲取一週的LINQ to SQL爲谷歌圖表

var week = DateTime.Now.Date.AddDays(-7);     

    List<LeaveChartVM> data = new List<LeaveChartVM>(); 
    using (ApplicationDbContext db = new ApplicationDbContext()) 
    {   
     data = (from u in db.UserProfiles 
       select new LeaveChartVM 
       { 
       Users = (from up in db.UserProfiles 
         select up).Count(), 

       OnLeaveToday = (from tol in db.CalendarData 
           where (today >= tol.StartTime && today <= tol.EndTime) 
           select tol).Count(),          

       SickLeave = (from sl in db.CalendarData 
          where sl.LeaveType == "5" 
          select sl).Count(), 

       AnnualLeave = (from al in db.CalendarData 
           where al.LeaveType == "1" 
           select al).Count() 
      }).Distinct().ToList(); 
    } 

能有人幫我只有這個星期返回的數據嗎?我設法返回今天的疾病並離開,但我想創建一個線性圖表,返回本週的數據。

添加到這是信息wasnt很大,繼承人的表結構的calendarDatas -

[Subject] 
    ,[Description] 
    ,[StartTime] 
    ,[EndTime] 
    ,[AllDay] 
    ,[Recurrence] 
    ,[RecurrenceRule] 
    ,[LeaveType] 
    ,[StartTimeZone] 
    ,[EndTimeZone] 
    ,[UserId] 
    ,[EventStatus] 
    ,[ApporovedId] 
    ,[ApprovedDate] 
    ,[Duration] 
    ,[EventAction] 
    ,[EventDate] 
    ,[uid] 
    ,[colourId] 
    ,[UserProfiles_Id] 

,然後也爲的UserProfiles -

​​

什麼,我想回是人都上年度過去7個工作日以及當天的假和病假。

+0

請張貼*有關*代碼只和的CalendarData的結構。當我們甚至不知道您是否有* DateTime列時,如何獲得某個星期的數據是不可能的。 –

+0

我認爲它不是內置的。 您可以製作if語句。 如果sonday ...得到6天后... 如果週一...之前拿到的一天後,5天... ...... – 2016-08-18 09:37:57

+0

@SimonR - 你的代碼是混亂。您正在爲每個用戶選擇不同的東西,而無需將其「綁定」到特定用戶。 +這個問題本身約1天與一週。請說出你想要的數據輸出是什麼,以及不同的表格如何看起來像 –

回答

1

像下面這樣的東西?

DateTime today = DateTime.Now.Date; 
DateTime oneWeekAgo = today.AddDays(-7); 

// usage 
OnLeaveThisWeek = 
    (from tol in db.CalendarData 
    where (tol.StartTime >= oneWeekAgo && tol.EndTime <= today) 
    select tol).Count() 

確保您todayoneWeekAgo值你所需要的邏輯,即今天是否包括它是不是等匹配

+0

謝謝,這讓我想要什麼。 – SimonR