Samw
Samw

Reputation: 128

c# get all dates from database that appear with current week

trying to get all the dates from the db that are within the current week

i have email1DueDate, email2DueDate, email3DueDate

and i want to see if they are due to be sent within the current week

Upvotes: 0

Views: 1833

Answers (2)

musefan
musefan

Reputation: 48445

First you should calculate two DateTime values for the start of the week and the end of the week, you can do that like this (assuming Monday is the first day):

DateTime startOfWeek = DateTime.Today;
int delta = DayOfWeek.Monday - startOfWeek.DayOfWeek;
startOfWeek = startOfWeek.AddDays(delta);

DateTime endOfWeek = startOfWeek.AddDays(7);

next you can use this in your LINQ query to get the results you need, I am assuming you want results to be rows that have any of your due dates fall in the week:

var results = DB.Table.Where(x =>
    (x.email1DueDate >= startOfWeek && x.email1DueDate < endOfWeek) ||
    (x.email2DueDate >= startOfWeek && x.email2DueDate < endOfWeek) ||
    (x.email3DueDate >= startOfWeek && x.email3DueDate < endOfWeek)
    );

If this is not what you need then you will need to clarify your requirements

Upvotes: 7

bjornruysen
bjornruysen

Reputation: 850

LINQ

listOfDates.Where(x => x.email1DueDate > beginOfWeekDate && x.email1DueDate < endOfWeekDate).ToList();

Of course you'll still have to figure out the begin and end dates

Upvotes: 1

Related Questions