linq to sql how to retrieve most recent records based on datetime

Posted on 2011-05-13
Last Modified: 2013-11-11

List<CustomersList> thisList  = linqProvider.All<Customer>().Take(20).ToList<Customer>();

The customer table has a datetime field and I want to get the 20 most recent records.  How can I do that?
Question by:ToString1
    LVL 1

    Expert Comment

     var q = from c in db.Contact
               where c.DateOfBirth.AddYears(35) > DateTime.Now
               orderby c.DateOfBirth descending
               select c;

    Open in new window

    LVL 16

    Expert Comment

    by:Imran Javed Zia
    just use order by datetime field and get 20 records

    Author Comment

    So will order by get the most recent?
    LVL 21

    Accepted Solution

    Try something like this:

    List<CustomersList> thisList  = linqProvider.All<Customer>().OrderByDescending(x => x.YourDateField).Take(20).ToList<Customer>();


    Write Comment

    Please enter a first name

    Please enter a last name

    We will never share this with anyone.

    Featured Post

    Looking for New Ways to Advertise?

    Engage with tech pros in our community with native advertising, as a Vendor Expert, and more.

    Suggested Solutions

    A quick way to get a menu to work on our website, is using the Menu control and assign it to a web.sitemap using SiteMapDataSource. Example of web.sitemap file: (CODE) Sample code to add to the page menu: (CODE) Running the application, we wi…
    More often than not, we developers are confronted with a need: a need to make some kind of magic happen via code. Whether it is for a client, for the boss, or for our own personal projects, the need must be satisfied. Most of the time, the Framework…
    To add imagery to an HTML email signature, you have two options available to you. You can either add a logo/image by embedding it directly into the signature or hosting it externally and linking to it. The vast majority of email clients display l…
    In this tutorial you'll learn about bandwidth monitoring with flows and packet sniffing with our network monitoring solution PRTG Network Monitor ( If you're interested in additional methods for monitoring bandwidt…

    779 members asked questions and received personalized solutions in the past 7 days.

    Join the community of 500,000 technology professionals and ask your questions.

    Join & Ask a Question

    Need Help in Real-Time?

    Connect with top rated Experts

    15 Experts available now in Live!

    Get 1:1 Help Now