Ayman Ayman - 1 year ago 121
ASP.NET (C#) Question

Getting n number of row value in string format from datatable object

The below data-table has this below structure.

RCDID EmployeeID LogDate LogTime TerminalID InOut
1682284 362426 07/01/2017 08:38:46 HO001 0
1682286 362426 07/02/2017 08:32:04 HO001 0
1682287 362426 07/02/2017 08:32:06 HO001 0
1682289 362426 07/03/2017 08:35:08 HO001 0
1682291 362426 07/04/2017 08:38:23 HO001 0
1682292 362426 07/04/2017 08:38:25 HO001 0


How can i retrieve the second value of LogTime for certain date in form of string.

for example for date
07/02/2017
retrieved value will be
08:32:06
.

and for 07/04/2017 retrieved value will be
08:38:25

SeM SeM
Answer Source

Try this:

string logTime = dataTable.AsEnumerable()
                          .Where(d => d.Field<DateTime>("LogDate").Date == new DateTime(2017, 4, 7).Date)
                          .Select(s => s.Field<string>("LogTime"))
                          .ToList()[1];

this will return the second value from your table if the LogTime is string, if not change s.Field<string>("LogTime") part to match your data type.

Recommended from our users: Dynamic Network Monitoring from WhatsUp Gold from IPSwitch. Free Download