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 7624231
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 31, 20262026-05-31T04:54:31+00:00 2026-05-31T04:54:31+00:00

I have a table AppointmentStatusHistory in the following format: AppointmentId AppointmentStatusId Date ======================================================== 1

  • 0

I have a table AppointmentStatusHistory in the following format:

AppointmentId   AppointmentStatusId   Date
========================================================
1               1                     2/1/2012 (2nd January)
1               2                     2/2/2012 (2nd February)

I currently run a query against this to return the ‘most recent’ status for the appointment within a given timeframe.

My LINQ Query

items = (from t1 in db.AppointmentStatusHistories
         from t2 in db.TaskAppointments
                      .Where(o => (o.ID == t1.AppointmentId))
         from t3 in db.Tasks
                      .Where(o => (o.Task_ID == t2.Parent_Task_ID))
         where t1.Timestamp >= first && t1.Timestamp <= last
            && t3.Creator == user
         group t1 by t1.AppointmentId into grp
         select new UserAppointmentTargetModel
         {
             AppointmentId = grp.Key,
             AppointmentStatusId = grp.OrderByDescending(g => g.Timestamp)
                                      .FirstOrDefault()
                                      .AppointmentStatusId
    }
);

Using the above returns AppointmentStatusId status of ‘1’ when first=1/1/2012 and last=2/1/2012.

Requirements

I hoped somebody may be able to give me some advice on amending this to meet the following conditions:

  • If the most up to date status is within the current period, include the record.
  • If not, omit it from the result set.
  • 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-31T04:54:33+00:00Added an answer on May 31, 2026 at 4:54 am

    You just need to move the last part of the filtering to after the grouping/winnerpicking.

    db.AppointmentStatusHistories
    .Where(ash => first <= ash.TimeStamp) //omit all records that are before the range
    .Where(ash => ash.Appointment.TaskAppointments.Any(ta => ta.Task.Creator == user))
    .GroupBy(ash => ash.AppointmentId)
    .Select(g => g.OrderByDescending(ash => ash.TimeStamp).First()) //pick a winner
    .Where(ash => ash.TimeStamp <= last) //omit winners that are after the range
    .Select(ash => new UserAppointmentTargetModel()
    {
      AppointmentId = ash.AppointmentId,
      AppoinementStatusId = ash.AppointmentStatus,
      Date = ash.Timestamp
    }
    

    (obligatory query comprehension syntax form of the above)

    from ash in db.AppointmentStatusHistories
    where first <= ash.TimeStamp
    where ash.Appointment.TaskAppointments.Any(ta => ta.Task.Creator == user)
    group ash by ash.AppointmentId into g
    let winner = g.OrderByDescending(ash => ash.TimeStamp).First()
    where winner.TimeStamp <= last
    select new UserAppointmentTargetModel()
    {
      AppointmentId = winner.AppointmentId,
      AppoinementStatusId = winner.AppointmentStatus,
      Date = winner.Timestamp
    }
    

    Side notes:

    I used navigation properties to do the user filtering. If you can’t get that to work, go back to the joins.

    It’s always safe to call First on a Group. Groups aren’t ever empty. There’s no need for FirstOrDefault in this case.

    I reused the ash variable name in the method style query to communicate type since it is declared in two different places where the type is not stated. I changed that to winner in the comprehension style query to communicate intent better since it is declared in one place where the type may be verified by inspection.

    Also, I never use >= with dates. It can only lead to sorrow.

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

Sidebar

Related Questions

I have table the following data structure in SQL Server: ID Date Allocation 1,
I have table like below ID User Date Location 1 Tom 6-Mar-2012 US 2
I have table table1 **************************** id | start_date | end_date 1 30/06/2012 01/01/9999 When
I have table tree. I have query: SELECT * FROM tree WHERE pid =10
I have table v_c_country with column table_name I can make an query and, as
I have table with data about tasks like Id, Name, Date, WorkerId, VehicleId (and
I have table with around 70 000 rows. There is 6000 rows that i
I have table in which I am inserting rows for employee but next time
I have table and this table contain result column with some entries. I just
I have table with 300 000 records (MyISAM). I get record from this table

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.