Skip to content

Instantly share code, notes, and snippets.

@jacobtoye
Created March 23, 2017 00:13
Show Gist options
  • Save jacobtoye/9645c847c8b077a13b36700f1be16b5d to your computer and use it in GitHub Desktop.
Save jacobtoye/9645c847c8b077a13b36700f1be16b5d to your computer and use it in GitHub Desktop.
public static class OverspeedPeriodExtensions
{
public static IQueryable<OverspeedPeriod> WithRemoteId(this IQueryable<OverspeedPeriod> overspeedPeriods, params int[] remoteIds)
{
return overspeedPeriods.Where(l => remoteIds.Contains(l.RemoteId));
}
/// <summary>
/// Filters by all periods that start within date range.
///
/// E.g. returns C and D
///
/// | |
/// A [---] | |
/// B [-+-] |
/// C | [---] |
/// D | [-+-]
/// E | |
/// F | | [---]
/// G [-+-----------+-]
/// | |
///
/// </summary>
public static IQueryable<OverspeedPeriod> StartsWithin(this IQueryable<OverspeedPeriod> overspeedPeriods, DateTime minDate, DateTime maxDate)
{
return overspeedPeriods.Where(p => p.StartTime >= minDate && p.StartTime < maxDate);
}
/// <summary>
/// Filters by all periods that end within date range.
///
/// E.g. returns B and C
///
/// | |
/// A [---] | |
/// B [-+-] |
/// C | [---] |
/// D | [-+-]
/// E | |
/// F | | [---]
/// G [-+-----------+-]
/// | |
///
/// </summary>
public static IQueryable<OverspeedPeriod> EndsWithin(this IQueryable<OverspeedPeriod> overspeedPeriods, DateTime minDate, DateTime maxDate)
{
return overspeedPeriods.Where(p => p.EndTime >= minDate && p.EndTime < maxDate);
}
/// <summary>
/// Filters by all periods that are only fully contained within date range.
///
/// E.g. returns only C
///
/// | |
/// A [---] | |
/// B [-+-] |
/// C | [---] |
/// D | [-+-]
/// E | |
/// F | | [---]
/// G [-+-----------+-]
/// | |
///
/// </summary>
public static IQueryable<OverspeedPeriod> ContainedWithin(this IQueryable<OverspeedPeriod> overspeedPeriods, DateTime minDate, DateTime maxDate)
{
return overspeedPeriods.Where(p => p.StartTime >= minDate && p.EndTime < maxDate);
}
/// <summary>
/// Gets all overspeed periods that either starts within, ends with, is contained or overlaps date range.
///
/// E.g. return B, C, D, G
///
/// | |
/// A [---] | |
/// B [-+-] |
/// C | [---] |
/// D | [-+-]
/// E | |
/// F | | [---]
/// G [-+-----------+-]
/// | |
///
/// </summary>
public static IQueryable<OverspeedPeriod> Intersects(this IQueryable<OverspeedPeriod> overspeedPeriods, DateTime minDate, DateTime maxDate)
{
return overspeedPeriods.Where(p => p.EndTime >= minDate && p.StartTime < maxDate);
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment