Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 7044847
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 28, 20262026-05-28T02:27:30+00:00 2026-05-28T02:27:30+00:00

Not sure how best to explain this, so bear with me… I’m writing an

  • 0

Not sure how best to explain this, so bear with me…

I’m writing an application to auto generate a staff rota.

I have a table (tl_RotaDays) which is essentially a calendar which records which staff member was on call that particular day – eg.

Date           Login
---------------------------
02-01-2012     Bob
03-01-2012     Bob 
04-01-2012     Bob 
05-01-2012     Bob 
06-01-2012     Bob 

09-01-2012     Bob 
10-01-2012     Bob 
11-01-2012     Bob 
12-01-2012     Sue 
13-01-2012     Sue 

I want to return the date of the Friday in the last “full” working (Mon-Fri) week each person worked, based on a “full” week being defined as a week in which they worked >=3 days.

As such in the above example, the query for “Bob” should return “13-01-2012”, as he worked the majority of that week, even though Sue worked the end of the week.

I have a query which gets the last date each person worked:

lastEveningOOH = Convert.ToDateTime((from rd in db.tl_rotaDays
                                where rd.ooh == tc.login && ((int)Convert.ToDateTime(rd.date).DayOfWeek >= 1 &&
                                            (int)Convert.ToDateTime(rd.date).DayOfWeek <= 5)
                                select rd.date).Max()),

But the only way I can think to check if the person worked a “full” week is to query the original table against each line, which seems frightfully inefficient.

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-05-28T02:27:30+00:00Added an answer on May 28, 2026 at 2:27 am

    If dt is the list of dates you can do it in one Linq-Query like this:

    DateTime maxWeek =
                dt.Where(d => d.DayOfWeek != DayOfWeek.Saturday && d.DayOfWeek != DayOfWeek.Sunday).GroupBy(
                    d => new GregorianCalendar().GetWeekOfYear(d, CalendarWeekRule.FirstFourDayWeek, DayOfWeek.Monday)).Where(
                        g => g.Count() > 2).OrderBy(g => g.Key).Last().Max();
    

    If the purpose is to optimize a SQL query you can check what Linq2Sql generates here and see if not a simple algorithmic approach (look at maximum date => check if full week => look at maximum date – 7 etc.) works best.
    Also the above Linq query will possibly be incorrect if the dates span multiple years, but the grouping can be modified to rectify that.

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I'm not sure how to best explain this so I have linked to a
Not sure of the best way to explain this... I have an input where
I'm really not sure on the best way to explain this, but we have
I'm not sure how to best explain this, so this may be a bit
Not sure the best way to explain this but i'll give it a shot.
I'm not too sure how to explain this, but I will try. I have
I've got a problem I'm not sure how best to solve. I have an
I'm not quite sure the best way to deal with this situation. Here's a
So I'm not sure what the best way to accomplish this is, but basically
Alright, I'll try to explain this the best I can. I have an iPhone

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.