Broge Broge - 1 month ago 15
C# Question

Query range of data linq c#

I have a calendar in my website and users can create events of one or more days.

So, in my

table I set two fields
(Datetime) and

I want to do a query with a specific date range like from

Now, how can I take all the events that are in this range?

I have to take:

  • Events that start in this range and end before or after the range end,

  • Events that start before the range and end in this range,

  • Events that start before this range but end after this range

Is it possible to do a single query or i have to do three different query?

I hope I explained myself, otherwise ask me...

Answer Source

I would say it should be enough to test for event.End >= range.Start && event.Start <= range.End

       0 1 2 3 4 5 6 7 8 9
Range        |-----|
Evt1   |-|                   evt.End < range.Start => false
Evt2                 |-|     evt.Start > range.End => false
Evt3     |-----------|       => true
Evt4     |-------|           => true
Evt5             |-----|     => true
Evt6           |-|           => true