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?
ToString1Asked:
Who is Participating?
 
Alfred A.Connect With a Mentor Commented:
Try something like this:

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




0
 
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 ZiaConsultant Software Engineer - .NET ArchitectCommented:
just use order by datetime field and get 20 records
0
 
ToString1Author Commented:
So will order by get the most recent?
0
Question has a verified solution.

Are you are experiencing a similar issue? Get a personalized answer when you ask a related question.

Have a better answer? Share it in a comment.

All Courses

From novice to tech pro — start learning today.