Mark Tabor

Mark Tabor

  • 587
  • 2k
  • 458.2k

How to get one record from list with condition of Date Descending

Aug 15 2022 9:12 AM

Dear all I have a table with the following columns , Now I have two three rows with Department 

Employee Table

Emp_ID First_Name Last_Name
1 NEW OLD
2 FOG HOP

 

Employee Detail table

Table_ID Emp_ID Department Salary Visit_Date
1 1 HR 1200 5/5/2021
2 2 HR 1300 8-8-2022
3 3 ICT 1400 01-01-2022

 

Now I want to get the employee detail with name Department,salary,Visit_Date I want to get only HR department employee and from HR only one Row whose date is latest date like 8-8-2022

below is my current query.

var MyQuery=(from nn in db.EmployeeDetail 

                       from vv in db.Employee

                       where vv.Emp_ID=nn.Emp_ID

                       order by vv.department 

select new EmployeeDetail

{

visit_date=nn.Visit_Date,

Salary=nn.Salary).FirstOrDefault(),

}).ToList();

but it return two rows of HR i need one row with latest visit Date.

}


Answers (6)