Hey, I have a table that has multiple records with a start date (PlacementDate) and end date (Pulled Date). I'm passing start date and end date parameters into this function which needs to return the records that intersect the start and end date passed in and also specify how many days each record intersects for.
Getting the records that intersect is easy, getting the number of days of intersection between the date ranges is not so easy... Here's what I have so far:
var query = from d in db.TelemetryDevices
join p in db.DevicePlacements on d.TelemetryDeviceID equals p.TelemetryDeviceID
where d.CompanyID == companyId && d.BillingPlanID == billingPlanId
&& (
(p.PlacementDate <= startDate && (p.PulledDate != null ? p.PulledDate.Value : DateTime.MaxValue) >= endDate)// { start - end }
|| (p.PlacementDate <= startDate && (p.PulledDate != null ? p.PulledDate.Value : DateTime.MaxValue) >= startDate)// { start } end
|| (p.PlacementDate >= startDate && (p.PulledDate != null ? p.PulledDate.Value : DateTime.MaxValue) <= endDate)// start { } end
|| (p.PlacementDate >= startDate && (p.PulledDate != null ? p.PulledDate.Value : DateTime.MaxValue) >= endDate)// start { end }
)
select new DeviceView
{
TelemetryDeviceID = d.TelemetryDeviceID
};
Any ideas as to how to solve this would be greatly appreciated.