vaibhav shah
vaibhav shah

Reputation: 5069

Find data for latest Inserted date using linq in c#, mysql

I have This table "CommonData" in my mySql table

enter image description here

I want to query from c# using linq to find list of "Id" from latest inserted date. i.e. Here Latest Insert Date is "22-04-2013" , so I should get list of Ids whos insert date is "22-04-2013".

I am using Linq for it.

Some how I am not able to do this.

Upvotes: 1

Views: 617

Answers (2)

Ashwini Verma
Ashwini Verma

Reputation: 7525

simple one might be using OrderByDescending

var result = CommonData.OrderByDescending(c=> c.InsertDate).Select(c => c.Id);

Upvotes: 0

Cyril Gandon
Cyril Gandon

Reputation: 17048

Add a subquery retrieving the maximum date, and then filter on this date:

var ids = ctx.CommonDatas
             .Where(c => c.InsertDate == 
                               ctx.CommonDatas
                                  .Max(c2 => c2.InsertDate)
                   )
             .Select(c => c.Id);

Upvotes: 4

Related Questions