2008-12-08 39 views
3

有幾個關於LIN已經有關於LINQ樞軸的問題,雖然其中有幾個概述了我的確切問題,但我無法成功地將它們轉換爲工作解決方案。我覺得這主要是由於我加入了我的桌子。另一個LINQ樞軸問題 - 將SQL腳本轉換爲LINQ

因此,爲了所有LINQ愛好者都喜歡的問題,這裏有另一個難題讓你解決問題。請幫我(和掙一些信譽分,離我很尊重)下面的SQL存儲過程腳本轉換爲LINQ:

ALTER PROCEDURE [dbo].[GetTimesheetForWeekById] 
    @timesheetid int, 
    @begindate VarChar(20), 
    @enddate VarChar(20) 
AS 
BEGIN 

    SELECT T.TaskName, 
     SUM(
      case DATEPART(weekday, TE.StartTime) 
       WHEN 1 THEN DATEDIFF(minute, TE.StartTime, TE.EndTime) ELSE 0 END 
      ) AS Sunday, 
     SUM(
      case DATEPART(weekday, TE.StartTime) 
       when 2 THEN DATEDIFF(minute, TE.StartTime, TE.EndTime) ELSE 0 END 
      ) AS Monday, 
     SUM(
      case DATEPART(weekday, TE.StartTime) 
       when 3 THEN DATEDIFF(minute, TE.StartTime, TE.EndTime) ELSE 0 END 
      ) AS Tuesday, 
     SUM(
      case DATEPART(weekday, TE.StartTime) 
       when 4 THEN DATEDIFF(minute, TE.StartTime, TE.EndTime) ELSE 0 END 
      ) AS Wednesday, 
     SUM(
      case DATEPART(weekday, TE.StartTime) 
       when 5 THEN DATEDIFF(minute, TE.StartTime, TE.EndTime) ELSE 0 END 
      ) AS Thursday, 
     SUM(
      case DATEPART(weekday, TE.StartTime) 
       when 6 THEN DATEDIFF(minute, TE.StartTime, TE.EndTime) ELSE 0 END 
      ) AS Friday, 
     SUM(
      case DATEPART(weekday, TE.StartTime) 
       when 6 THEN DATEDIFF(minute, TE.StartTime, TE.EndTime) ELSE 0 END 
      ) AS Saturday 

    FROM Tasks T 
    INNER JOIN TimeEntries TE on T.TaskID = TE.TaskID 
    WHERE TE.StartTime BETWEEN 
     (CONVERT(datetime, @begindate, 103)) AND (CONVERT(datetime, @enddate, 103)) 
    AND TE.TimesheetID = @timesheetid 
    GROUP BY T.TaskName 
END 

回答

1

好吧,假設外鍵的對象表示,類似:

 int timesheetId = ... 
     DateTime start = ..., end = ... 
     var qry = from timeEntry in ctx.TimeEntries 
       let date = timeEntry.StartTime.Date 
       where timeEntry.TimesheetId == timesheetId 
       && date >= start 
       && date <= end 
       group timeEntry by timeEntry.Task.TaskName into grp 
       select new { 
        TaskName = grp.Key, 
        Monday = grp.Where(x => x.StartTime.DayOfWeek == DayOfWeek.Monday).Count(), 
        Tuesday = grp.Where(x => x.StartTime.DayOfWeek == DayOfWeek.Tuesday).Count(), 
        Wednesday = grp.Where(x => x.StartTime.DayOfWeek == DayOfWeek.Wednesday).Count(), 
        Thursday = grp.Where(x => x.StartTime.DayOfWeek == DayOfWeek.Thursday).Count(), 
        Friday = grp.Where(x => x.StartTime.DayOfWeek == DayOfWeek.Friday).Count() 
       }; 

不幸的是,一些細節取決於SQL提供者 - 即哪些函數(如DayOfWeek等)可以成功映射爲TSQL。讓我知道如果你的問題......

+0

非也!只有一個問題 - 我需要一週中的每一天而不是數。但是這絕對是答案,但是這個例外。下一條評論包含例外情況。 – 2008-12-08 09:19:03

+0

System.Collections.Generic.IEnumerable 不包含「Sum」的定義和最佳擴展方法重載「System.Linq.Enumerable.Sum(System.Collections.Generic.IEnumerable )」有一些無效論據 – 2008-12-08 09:20:21

1

這個怎麼樣(修改Marc的):

var qry = from timeEntry in timeEntries 
     let date = timeEntry.StartTime.Date 
     where timeEntry.TimesheetId == timesheetId 
     && date >= start 
     && date <= end 
     group timeEntry by timeEntry.Task.TaskName into grp 
     select new 
     { 
      TaskName = grp.Key, 
      Monday = grp.Where(x => x.StartTime.DayOfWeek == DayOfWeek.Monday) 
        .Sum(x=>x.EndTime.Subtract(x.StartTime).TotalMinutes), 
      Tuesday = grp.Where(x => x.StartTime.DayOfWeek == DayOfWeek.Tuesday) 
        .Sum(x => x.EndTime.Subtract(x.StartTime).TotalMinutes), 
      Wednesday = grp.Where(x => x.StartTime.DayOfWeek == DayOfWeek.Wednesday) 
         .Sum(x => x.EndTime.Subtract(x.StartTime).TotalMinutes), 
      Thursday = grp.Where(x => x.StartTime.DayOfWeek == DayOfWeek.Thursday) 
        .Sum(x => x.EndTime.Subtract(x.StartTime).TotalMinutes), 
      Friday = grp.Where(x => x.StartTime.DayOfWeek == DayOfWeek.Friday) 
        .Sum(x => x.EndTime.Subtract(x.StartTime).TotalMinutes), 
     };