[Okta Webinar] Learn how to a build a cloud-first strategyRegister Now

x
  • Status: Solved
  • Priority: Medium
  • Security: Public
  • Views: 346
  • Last Modified:

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



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?
0
ToString1
Asked:
ToString1
1 Solution
 
nidheeshmcaCommented:
 var q = from c in db.Contact
           where c.DateOfBirth.AddYears(35) > DateTime.Now
           orderby c.DateOfBirth descending
           select c;

Open in new window

0
 
Imran Javed ZiaCommented:
just use order by datetime field and get 20 records
0
 
ToString1Author Commented:
So will order by get the most recent?
0
 
Alfred A.Commented:
Try something like this:

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




0

Featured Post

Fill in the form and get your FREE NFR key NOW!

Veeam is happy to provide a FREE NFR server license to certified engineers, trainers, and bloggers.  It allows for the non‑production use of Veeam Agent for Microsoft Windows. This license is valid for five workstations and two servers.

Tackle projects and never again get stuck behind a technical roadblock.
Join Now