-1

So I only have a CreatedDate to work with.

I have a scheduled task that runs everyday at 12 midnight and that needs to get all records that fall on the same day of the month (but not if the CreatedDate == DateTime.Now).

I would prefer this to be in LINQ since this is a database call but any iteration logic would suffice, I think.

I have this right now but this has problems with days higher than 28:

var results = Context.Subscriptions.Where(
                x => !(x.StartDate.Day == DateTime.Now.Day &&
                      x.StartDate.Month == DateTime.Now.Month &&
                      x.StartDate.Year == DateTime.Now.Year) && 
                     x.StartDate.Day == DateTime.Now.Day);

Updating the question for clarity:

For example, a record's created date is January 31 2013. This record should get pulled every month. But since not all months have 31 days, my code above won't work on those months. I still need to pull the record every month even if that month has no 31st.

vsebastian
  • 57
  • 6
  • How far have you gotten so far? Post your best effort and what you think is the best way to solve the problem. – dcaswell Sep 06 '13 at 02:24
  • 1
    So the records on the 31st only get pulled in 7 of the 12 months? When should they be pulled? – D Stanley Sep 06 '13 at 02:25
  • Posted what I have now. So far, if the start day falls in 29, 30 or 31, they will not get pulled every month. They should be pulled the next day I guess? So for example, if the record's created date falls on 31st and the current month only has 30 days, the record should get pulled next month on the 1st. If this is not hte best solution, I'm very open to other answers. – vsebastian Sep 06 '13 at 02:29
  • @Kablitz Why should the 1st be pulling records from the 30th or the 31st? It should be pulling records from the 1st, shouldn't it? Or do you need the 1st to also generate all remaining reports for days 29-31 that did not exist in the previous month? – doppelgreener Sep 06 '13 at 02:58

1 Answers1

0

Just a thought maybe you could utilize windows service in running a scheduled task but there's another way task scheduler in .net. If you are going to use windows service use can use the timer class to do repeated runs. And if you have a table of tasks you can have columns for intervaldatetime, datelastrundatetime, and nextjobdatetime. in here you can check for the interval of time it would be run and check for the datetime the job/task to be run or it should not run.

check this for more information: http://msdn.microsoft.com/en-us/magazine/cc163821.aspx

I hope it help you.

john carlo
  • 21
  • 2
  • I've already thought of something like this (the intervaldatetime, datelastrundatetime, and nextjobdatetime). But this data is pulled from the external service and we'd like to avoid that if possible. Also, my superior doesn't want to add those columns to our database, for reasons that I don't know. – vsebastian Sep 06 '13 at 03:28
  • Have you read this? http://stackoverflow.com/questions/5335681/how-to-schedule-jobs-without-overlap-using-linq-to-objects – john carlo Sep 06 '13 at 04:47