yatie (1) [Avatar] Offline
#1
Hello,

I have 1 database which is stored Service History for staff.
And i also use add button (to add service history for staff) and come out the latest service history such as Basic Salary , job and position. Not include Record Type (like Increment/Hired and so on)

How to select latest date automatically use linq. Pls help me dear.
Below is my code. Pls add select latest date for me. TQ.

Using db As New AutoPayDAL.AutoPayDataContext(sDBCon)

Dim Emaster As AutoPayDAL.ServiceHistory = (From rec In db.ServiceHistories _
Where rec.EmpNo = lblEmployee.Text And rec.Sequence = "1").SingleOrDefault
AldousA (4) [Avatar] Offline
#2
Re: Select Latest Date use Linq
I have something where I have a database with record date in it, I want to get a list of all record dates and then assin the latest one to something I use later on. Here is my code

Private Sub GetMonths()
Months = From m In DB.Tbl_l_dates _
Where m.Status <> "Not Loaded" _
Select m.Record_date
LatestMonth = CDate(Months.Max)

End Sub

If I wanted just the latest month then I could simply write:

LatestMonth = Aggregate r In Tbl_l_dates Into Max(r.Record_date)

So I would imagine in your case (assuming that ServiceDate is the Date member) something like the following would work:

Dim LatestService = Aggregate rec In db.ServiceHistories _
Where rec.EmpNo = lblEmployee.Text And rec.Sequence = "1" _
Into Max(rec.ServiceDate)