Note that there are some explanatory texts on larger screens.

plurals
  1. PO
    text
    copied!<p>Here's some code that will do the work, using Linq. I don't know if it's in the form you were looking for, but the logic works with the data you gave us. You ought to be able to rearrange it into the proper shape for your app.</p> <p><strong>MODIFIED</strong></p> <pre><code>var rows = new[] { new { Id = 1, Name="Mike", TaskName="task1", StartTime=DateTime.Parse("2009-07-28 09:00:00"), EndTime=DateTime.Parse("2009-07-28 09:45:00") }, new { Id = 2, Name="Mike", TaskName="task2", StartTime=DateTime.Parse("2009-07-28 09:30:00"), EndTime=DateTime.Parse("2009-07-28 09:40:00") }, new { Id = 3, Name="Mike", TaskName="task3", StartTime=DateTime.Parse("2009-07-28 09:50:00"), EndTime=DateTime.Parse("2009-07-28 10:30:00") }, new { Id = 4, Name="Mike", TaskName="task4", StartTime=DateTime.Parse("2009-07-28 10:20:00"), EndTime=DateTime.Parse("2009-07-28 10:30:00") }, new { Id = 5, Name="Mike", TaskName="task5", StartTime=DateTime.Parse("2009-07-28 10:40:00"), EndTime=DateTime.Parse("2009-07-28 11:50:00") }, }; var ranges = rows.Select(x =&gt; new { StartTime = x.StartTime, EndTime = x.EndTime }); int rangeCount = 0; while (ranges.Count() != rangeCount) { rangeCount = ranges.Count(); ranges = ranges.Select(x =&gt; new { StartTime = ranges.Where(y =&gt; (y.StartTime &lt;= x.StartTime &amp;&amp; y.EndTime &gt;= x.StartTime) || (y.StartTime &lt;= x.EndTime &amp;&amp; y.EndTime &gt;= x.EndTime)).Min(y =&gt; y.StartTime), EndTime = ranges.Where(y =&gt; (y.StartTime &lt;= x.StartTime &amp;&amp; y.EndTime &gt;= x.StartTime) || (y.StartTime &lt;= x.EndTime &amp;&amp; y.EndTime &gt;= x.EndTime)).Max(y =&gt; y.EndTime), }).Distinct().ToList(); // ToList required to avoid recursion issues } TimeSpan gapTime = ranges.Max(x =&gt; x.EndTime) - ranges.Min(x =&gt; x.StartTime) - new TimeSpan(ranges.Sum(x =&gt; (x.EndTime - x.StartTime).Ticks)); System.Diagnostics.Trace.WriteLine(gapTime); </code></pre>
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload