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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 19, 20262026-05-19T11:13:59+00:00 2026-05-19T11:13:59+00:00

var events = context.Events……… Events has a property ‘DueDate’ (Datetime) I want to select

  • 0

var events = context.Events………

Events has a property ‘DueDate’ (Datetime)

I want to select most recent 5 records, how to write such a query? Thanks

  • 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-19T11:14:00+00:00Added an answer on May 19, 2026 at 11:14 am

    If you are interested in the Events that are closest to day you could do this:

    DateTime today = DateTime.Now;
    var events = context.Events
                        .OrderBy(e => Math.Abs((today - e.DueDate).Days))
                        .Take(5)
                        .ToList();
    

    This takes the events that are the least number of days away from today (in the past OR future).

    If you wanted events that are most recent but only in the future, you could do:

    DateTime today = DateTime.Now;
    var events = context.Events
                        .Where(e => e.DueDate >= today)
                        .OrderBy(e => (e.DueDate - today).Days)
                        .Take(5)
                        .ToList();
    

    Edit:

    If you use LINQ to Entities DateTime manipulation is not directly supported, but if you use SQL server something like DateDiff should work (untested you’ll have to try out yourself):

    DateTime today = DateTime.Now;
    var events = context.Events
                        .Where(e => e.DueDate >= today)
                        .OrderBy(e => System.Data.Objects.SqlClient.SqlFunctions.DateDiff("d", e.DueDate, today))
                        .Take(5)
                        .ToList();
    

    If that doesn’t work you can always get your data first, then filter with LINQ to objects, which has the added benefits that you’re not tying yourself to SQL Server – but obviously it’s not very efficient:

    DateTime today = DateTime.Now;
    var futureEvents = context.Events
                              .Where(e => e.DueDate >= today);
                              .ToList();   
    
    var filteredEvent = futureEvents 
                        .OrderBy(e => (e.DueDate - today).Days)
                        .Take(5)
                        .ToList();
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

how can we make a date shown in bold format? var events = {};
I am creating a user input at one of the events: var throwConnectBox =
I understood how to create events, like that: var evt=document.createEvent('Event'); evt.initEvent('foo',true,true); window.dispatch(evt); Then anyone
I have this code: $.getJSON(Featured/getEvents, function(data){ $.each(data.events, function(i,event){ var title = event.title.substr(0,20); $(#title-+i).text(Text); if
I have a timer script: import flash.utils.Timer; import flash.events.TimerEvent; var secs:Number = 30;//second var
Events is not rendering in FullCalender? var calendar = $('#calendar').fullCalendar({ theme: true, header: {
public IList<Event> SearchEvents(DateTime fromDate, DateTime toDate, int categoryId, string eventName ) { var query
I wrote these lines in My Application start event: var mongo = new Mongo();
My JavaScript code is as follows: $(document).keydown(function(event){ var move, inter; clearInterval(inter); inter = setInterval(move
i can reference link with specific id with following code $(document).ready(function(){ $(a#example_link_id).click(function(event){ var url

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.