Search code examples
c#linq

How to remove first element from linq query


Im trying to consolidate a list of records of in and out times per day to the minimum number of records possible.

What i have done so far, is grouped up the lines into the groups they need to be in, and put the in and out times in a list for each day.

then i want to process the lists and add the first set of in and out lines onto a single line, then process the next entry and either create a new line or fill in the blanks of the previous line.

The bit im stuck with is removing the first item from the linq result after i have processed it.

happy to look at doing it a different way.

here is what i have:

       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;

Update - Working code - before performance changes

 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;

Solution

  • use the List.RemoveAt Method something like myList.RemoveAt(0) will remove the first item of your list