Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

LINQ - how to get next three business days excluding dates in table?

Tags:

linq

I need to find the next three available business days for a scheduling application. What is available depends upon excluded dates in a table. So...as long as the date is not in my table and not a Saturday or Sunday, I want the next three. I'd like to find an efficient way of doing this.

I need to return a List<DateTime>. The table is simple - ExcludedDates has an ID and a DateTime with the excluded date.

I'd like to have a single LINQ query expression but can't figure it out...thanks to all in advance and I apologize if this is trivial or obvious - it isn't to me.

like image 624
CircusNinja Avatar asked Jul 25 '11 18:07

CircusNinja


1 Answers

Try this...

  DateTime start = DateTime.Now.Date;
  var result = Enumerable.Range(1, 10) // make this '10' higher if necessary (I assume you only exclude non-workingdays like Christmas and Easter)
                    .Select(offset => start.AddDays(offset))
                    .Where(date => !( date.DayOfWeek == DayOfWeek.Saturday || date.DayOfWeek== DayOfWeek.Sunday))
                    .Where(d=> !exceptionTable.Any(date => date == d))
                    .Take(3).ToList();
like image 108
Chaim Zonnenberg Avatar answered Sep 18 '22 13:09

Chaim Zonnenberg