2014-10-30 80 views
-1

我試圖將每天的進出時間記錄列表合併到可能的最小記錄數中。如何從linq查詢中刪除第一個元素

到目前爲止,我所做的工作是將線條分組到他們需要進入的組中,並將每天的進出時間放在列表中。

然後我想要處理列表並將第一組輸入和輸出行添加到一行上,然後處理下一個輸入並創建一個新行或填入上一行的空白。

我堅持的位是我處理它後,從linq結果中刪除第一項。

高興地看着做不同的方式。

這裏是我有:

 List<LoginRecordLine> condensedLoginRecordLines = new List<LoginRecordLine>(); 
     List<LoginRecordLine> currentLoginRecordLines = GetLoginRecordsForLoginRecordReport(lowerDate, upperDate, sageDatabaseID, loggedInUserID); 

     var groupedLines = from LoginRecordLine line in currentLoginRecordLines 
          group line by new { line.TimesheetID, line.WorkPatternPayRateID } into g 
          select new 
          { 
           Lines = g, 
           TimesheetID = g.Key.TimesheetID, 
           PayRateID = g.Key.WorkPatternPayRateID 
          }; 

     foreach (var g in groupedLines) 
     { 
      var monTimes = from line in g.Lines 
          orderby line.MonTimeIn ascending 
          where line.MonTimeSpan != TimeSpan.Zero 
          select new 
          { 
           TimeIn = line.MonTimeIn, 
           TimeOut = line.MonTimeOut, 
           Timesheet = line.Timesheet, 
           PayRate = line.WorkPatternPayRate 
          }; 

      var tueTimes = //Same as monday 

      var wedTimes = //Same as monday 

      var thuTimes = //same as monday 

      var friTimes = //same as monday 

      var satTimes = //same as monday 

      var sunTimes = //same as monday 


      while (monTimes.Count() != 0 || tueTimes.Count() != 0 || wedTimes.Count() != 0 || thuTimes.Count() != 0 || friTimes.Count() != 0 || satTimes.Count() != 0 || sunTimes.Count() != 0) 
      { 
       LoginRecordLine condensedLine = new LoginRecordLine(); 

       if (monTimes.Count() >0) 
       { 
        condensedLine.MonTimeIn = monTimes.First().TimeIn; 
        condensedLine.MonTimeOut = monTimes.First().TimeOut; 
        condensedLine.Timesheet = monTimes.First().Timesheet; 
        condensedLine.WorkPatternPayRate = monTimes.First().PayRate; 

        //*************** REVELANT PART *************/ 
        //remove first item from monday list 
       } 

       // tue 

       // wed 

       // etc 
      } 
     } 

     return condensedLoginRecordLines; 

更新 - 工作代碼 - 之前的性能變化

List<LoginRecordLine> condensedLoginRecordLines = new List<LoginRecordLine>(); 

       List<LoginRecordLine> currentLoginRecordLines = GetLoginRecordsForLoginRecordReport(lowerDate, upperDate, sageDatabaseID, loggedInUserID); 

       var groupedLines = from LoginRecordLine line in currentLoginRecordLines 
            group line by new { line.TimesheetID, line.WorkPatternPayRateID } into g 
            select new 
            { 
             Lines = g, 
             TimesheetID = g.Key.TimesheetID, 
             PayRateID = g.Key.WorkPatternPayRateID 
            }; 

       foreach (var g in groupedLines) 
       { 
        var monTimes = (from line in g.Lines 
            orderby line.MonTimeIn ascending 
            where line.MonTimeSpan != TimeSpan.Zero 
            select new 
            { 
             TimeIn = line.MonTimeIn, 
             TimeOut = line.MonTimeOut, 
             Timesheet = line.Timesheet, 
             PayRate = line.WorkPatternPayRate 
            }).ToList(); 

      var tueTimes = //Same as monday 

      var wedTimes = //Same as monday 

      var thuTimes = //same as monday 

      var friTimes = //same as monday 

      var satTimes = //same as monday 

      var sunTimes = //same as monday 

        while (monTimes.Count != 0 || tueTimes.Count != 0 || wedTimes.Count != 0 || thuTimes.Count != 0 || friTimes.Count != 0 || satTimes.Count != 0 || sunTimes.Count != 0) 
        { 
         LoginRecordLine condensedLine = new LoginRecordLine(); 

         if (monTimes.Count >0) 
         { 
          condensedLine.MonTimeIn = monTimes.First().TimeIn; 
          condensedLine.MonTimeOut = monTimes.First().TimeOut; 
          condensedLine.Timesheet = monTimes.First().Timesheet; 
          condensedLine.WorkPatternPayRate = monTimes.First().PayRate; 

          condensedLoginRecordLines.Add(condensedLine); 

          monTimes.RemoveAt(0); 
         } 

         //etc 
        } 
       } 

       return condensedLoginRecordLines; 
+3

您曾看過'Skip'? – 2014-10-30 08:31:57

+2

首先,您的代碼中存在嚴重的性能問題:使用Count()時您使用它是錯誤的。用Any()替換它,否則你在每次迭代時調用你的LINQ查詢。也許你應該考慮使用ToList()來實現查詢。 – galenus 2014-10-30 08:32:39

+0

@KirkWoll - 暫時還沒有,如果索引中沒有內容可以跳過,它將如何工作?例如,所有列表不會有相同數量的條目,星期一可能有2個,但在星期二沒有或一個 – WraithNath 2014-10-30 08:33:23

回答

2

使用List.RemoveAt Method像myList.RemoveAt(0)將刪除的第一個項目你列表

+0

謝謝,我想列表可能是要走的路,只是不知道他們如何使用匿名類型。 PS,組查詢是好的,它在哪裏我認同代碼底部的行,我需要跳過。 – WraithNath 2014-10-30 08:39:42

+1

@WraithNath另外一個好主意是反轉訂單並從最後移除,而不需要移動列表中的所有項目。 – Magnus 2014-10-30 08:43:07

+0

@Magnus - 很酷,謝謝,我想它幾乎就在那裏。我發佈工作代碼,然後改變性能 – WraithNath 2014-10-30 08:44:28

0

你應該修改你的算法和數據結構。

對於匿名類型的查詢我想補充一個星期幾的財產,所以查詢將看起來像:

var condensedLoginRecordLines = monTimes 
    .Concat(tueTimes) 
    .Concat(wedTimes) 
    ..//etc 
    .Select(data => new CondensedLine { WeekDay = data.WeekDay, /* here all the properties are initialized */ }) 
    .ToList(); 

和:

var monTimes = from line in g.Lines 
         orderby line.MonTimeIn ascending 
         where line.MonTimeSpan != TimeSpan.Zero 
         select new 
         { 
          TimeIn = line.MonTimeIn, 
          TimeOut = line.MonTimeOut, 
          Timesheet = line.Timesheet, 
          PayRate = line.WorkPatternPayRate, 
          WeekDay = DayOfWeek.Monday 
         }; 

然後,最後循環將通過類似更換就這樣。

如果您仍然喜歡使用MonInTime,TueInTime等屬性,請將創建的CondensedLine移動到一個單獨的函數中,該函數在WeekDay上應用切換並僅初始化相關屬性。在這種情況下,您應該聲明一個私有類,而不是您當前使用的匿名類型,以便將方法中的信息傳遞給另一個類。

相關問題