Update the list object

I have list of object's and i want to update the the object's Break property where breakflag=1 with its next and previous record.

class EmployeeHour 
   int recordID{get;set;}
   string Break{get;set;}
   DateTime TimeIn {get;set;}
   DateTime TimeOut {get;set;}
   int BreakFlag{get;set;}

List<EmployeeHour> listEH=(from item in employeeHoursList
                      where item.BreakFlag==1     
                        select item).Foreach(itme=>item.Break=(employeeHoursList[i].Timeout-employeeHoursList[i].TimeIn).ToString()).ToList();

So here i just want set the break property with the Time diff. between TimeIn and TimeOut only for those objcet whoes breakflag is one. and TimeOut of that object where BeakFlag ==1 and TimeIn next to that object in the list.


It sounds like what you're trying to do is something like this:

List<EmployeeHour> listEH=
    (from item in employeeHoursList
     where item.BreakFlag == 1     
     select item).ToList();
listEH.ForEach(item => item.Break = (item.TimeOut - item.TimeIn).ToString());

This will modify the current item.Break with the current item's .TimeOut and .TimeIn.

Update from your updated question it seems like what want is to modify item.Break using either the previous or the next item in the list. In this case you probably shouldn't use Linq or a ForEach at all. A simple for loop would be cleaner (assuming employeeHoursList is a List<T>, array, or similar):

for(var i = 0; i < employeeHoursList.Count; i++)
    if (employeeHoursList[i].BreakFlag == 1 && (i + 1) < employeeHoursList.Count) 
        employeeHoursList[i].Break = (employeeHoursList[i].TimeOut - employeeHoursList[i + 1].TimeIn).ToString()

Need Your Help

rails rvm path error when logout terminal

ruby-on-rails ruby rvm

i have run a rails project on my server, and i use ssh to connect server to start/stop/restart rails project with terminal, if i do not close the terminal, it run well, but when i close the termina...

Post a file to a .php page from Oracle

php oracle plsql

On my website I have a .php script to which our customers can post orders.