Share via


Order by latest date created or modified

Question

Friday, January 13, 2012 4:47 PM

I need a linq query that will result in ordering my data by which one is the most current date the modified date or created date.

Say have object 1 created 1/10/2012 never modified object 2 modified 1/13/2012 object 3 modified 1/5/2012. So the order should come out object 2, object 1, object 3.

All replies (5)

Friday, January 20, 2012 9:11 PM âś…Answered

Here are a couple of answers Samir did give me a good lead though:

If comparing just dates that are not going to be null

return from workItems in workItemDataContext.Work_Items
                   orderby (DateTime.Compare(workItems.Created,workItems.Modified.GetValueOrDefault(new DateTime(1980,1,1)))<0?workItems.Created:workItems.Modified) descending
                   //orderby(workItems.Created>workItems.Modified?workItems.Created:workItems.Modified) descending
                   select workItems;
If comparing modified and created of an item
return from workItems in workItemDataContext.Work_Items
                   orderby (workItems.Modified==null?workItems.Created:workItems.Modified) descending
                   select workItems;

Saturday, January 14, 2012 2:10 PM | 1 vote

I need a linq query that will result in ordering my data by which one is the most current date the modified date or created date.

Say have object 1 created 1/10/2012 never modified object 2 modified 1/13/2012 object 3 modified 1/5/2012. So the order should come out object 2, object 1, object 3.

Hi

You can do it like that

Dim db As New DataContext    
Dim q   = From m In db.mytable 
          Select max_cre_mod = If (m.date_created > m.date_modified,m.date_created,m.date_modified),
                 m.field2,m.field3
          Order By max_cre_mod Descending

Friday, January 20, 2012 6:47 PM

Do have the c# equivalent?


Friday, January 20, 2012 8:33 PM

Do have the c# equivalent?

I don't know c#, I hope someone jump in and converted it to you.


Monday, January 23, 2012 2:30 AM

Hi Chrys Perdue,

I'm glad to hear that you have solved the issue, and thanks for sharing the solution. With your help, the forum will be better and better.

Best Regards

Allen Li [MSFT]
MSDN Community Support | Feedback to us