How to compare only month and day with datetime object

In my C # program, I ran into an obstacle where I have a table that stores date ranges (columns are the date range identifier (int), start date (DateTime) and end date (DateTime). I want to query the table and to return strings that belong only to a specific date range.I cannot use datetime.date, since this includes the year.

So, for example, I want to query a table and get all date ranges that are between 01-01 and 5-31.

I tried using the following lambda to query a table, but my result set is empty.

List<DateRanges> tempDateRangeList = dataContext .DateRanges .Where(r=>r.BeginDate.Month <= startDate.Month && r.EndDate.Month >= finishDate.Month) .ToList(); tempDateRangeList = tempDateRangeList.Where(r=>r.BeginDate.Day <= startDate.Day && r.EndDate.Day >= finishDate.Day) .ToList(); 

Does anyone have any suggestions on how I can do this?

Edit:

Examples of BeginDate and EndDate are lists such as:

BeginDate 1/1/2016, 5/25/2016, 9/11/2016

EndDates 05/24/2016, 10/10/2016, 12/31/2016

Filter Date: startDate = 12/8 finishDate = 12/12

Expected Result: Start September 9 End Date 12/31

+6
source share
2 answers

There are two cases in your condition - a month equal to the boundary month, in which case you should check the day number and another month in which you ignore the day. Therefore, the request:

 List<DateRanges> tempDateRangeList = dataContext.DateRanges.Where(r => ((r.BeginDate.Month < startDate.Month) || (r.BeginDate.Month == startDate.Month && r.BeginDate.Day <= startDate.Day)) && ((r.EndDate.Month > finishDate.Month) || (r.EndDate.Month == finishDate.Month) && r.EndDate.Day >= finsihDate.Day)) .ToList(); 

The condition is ugly and very difficult to follow, but covers all cases. This query returns all records that specify date ranges that completely fall under boundary dates.

If you want to find records that simply overlap (in whole or in part) with a filtering range, then the query will look like this:

 List<DateRanges> tempDateRangeList = dataContext.DateRanges.Where(r => ((r.BeginDate.Month < endDate.Month) || (r.BeginDate.Month == endDate.Month && r.BeginDate.Day <= endDate.Day)) && ((r.EndDate.Month > startDate.Month) || (r.EndDate.Month == startDate.Month) && r.EndDate.Day >= startDate.Day)) .ToList(); 

This condition may bend your mind, but it works great.

+2
source

If the lambda expression is optional, I used linq queries (because that was the first solution I was thinking about).

 var validRanges = from range in ranges where range.BeginDate.CompareTo(startDate) <= 0 where range.EndDate.CompareTo(endDate) >= 0 select range; 

Using CompareTo is the easiest way to compare two DateTime structures. I invite you to look here for a full description of the method.

Edit

If you are not interested in the clock of your dates, but only in Day and Month, you should use range.BeginDate.Date.CompareTo(startDate.Date) and range.EndDate.Date.CompareTo(endDate.Date)

0
source

Source: https://habr.com/ru/post/1012984/


All Articles