Calculate the duration between the first entry and the last exit from a dataset in a datatable

I am reading an Excel document using ADO.Net in a dataset. Dataset Contains a set of employee records that contain login and logout times as Datatable. I need the first employee accounts and the most recent logon time to be the final record from the recordset based on the employee id and date. Here is my example data:

Emp Id Name Login Logout 12345 RAMACHANDRAN 7/30/2013 8:40 7/30/2013 10:40 12345 RAMACHANDRAN 7/30/2013 12:30 7/30/2013 14:20 12345 RAMACHANDRAN 8/01/2013 18:10 8/01/2013 20:20 12345 RAMACHANDRAN 8/01/2013 20:40 8/01/2013 22:00 12346 RAVI 8/03/2013 12:30 8/03/2013 14:20 12346 RAVI 8/03/2013 18:10 8/03/2013 20:20 

I need a final recording with a calculated duration

  Emp Id Name Login Logout Duration 12345 RAMACHANDRAN 7/30/2013 8:40 7/30/2013 14:20 5:40 12345 RAMACHANDRAN 8/01/2013 18:10 8/01/2013 22:00 3:50 12346 RAVI 8/03/2013 12:30 8/03/2013 20:20 7:50 

Is this the best way to use Linq to query this dataset? Which way is the best

+6
source share
2 answers

Something like that:

 dt.GroupBy(l => new {l.EmpId, l.Login.Date}, (key, g)=> new { EmpId = key.EmpId, Name = g.First().Name, Login = g.Min(d => d.Login), Logout = g.Max(d => d.Logout), Duration = g.Min(d => d.Login) - g.Max(d => d.Logout) }); 
+4
source

try this, I should use linq with the list below:

  var ListValues = (from fDate in db.tblLog select fDate).Tolist(); foreach (var item in ListValues) { DateTime in = Convert.ToDateTime(inDate ); DateTime out = Convert.ToDateTime(outDate ); TimeSpan ts = in - out; var duration = ts.TotalHours; } 
+1
source

Source: https://habr.com/ru/post/953681/


All Articles