Can you run my test code?
If so, I will modify the linq for the two-stage query.
If not, we will need to look at something else.
...or it could ACTUALLY be the .ToList()
using System;
using System.Collections.Generic;
using System.Linq;
namespace DW_399101
{
public class Staff
{
public int StaffID { get; set; }
public string FirstName { get; set; }
public string LastName { get; set; }
public string Address { get; set; }
public string Town { get; set; }
public string PostCode { get; set; }
public DateTime YearJoined { get; set; }
public char CurrentGrade { get; set; }
public string Role { get; set; }
public Staff()
{
StaffID = 0;
FirstName = "";
LastName = "";
Address = "";
Town = "";
PostCode = "";
YearJoined = new DateTime();
CurrentGrade = '\0';
Role = "";
}
}
public class Rating
{
public int RatingID { get; set; }
public int StaffID { get; set; }
public DateTime Date { get; set; }
public string Rating1 { get; set; }
public string Comment { get; set; }
public Rating()
{
RatingID = 0;
StaffID = 0;
Date = new DateTime();
Rating1 = "";
Comment = "";
}
}
public class CStaffDB
{
public List<Staff> Staffs { get; set; }
public List<Rating> Ratings { get; set; }
public CStaffDB()
{
Staffs = new List<Staff>();
Ratings = new List<Rating>();
}
}
public class CDW_399101
{
static void Main(string[] args)
{
List<Staff> lstStaff = new List<Staff>()
{
new Staff(){StaffID =1, CurrentGrade='E', FirstName = "Joe", LastName = "Jan"},
new Staff(){StaffID =2, CurrentGrade='C', FirstName = "Abe", LastName = "Arn"},
new Staff(){StaffID =3, CurrentGrade='J', FirstName = "Ben", LastName = "Bow"}
};
List<Rating> lstRatings = new List<Rating>()
{
new Rating(){StaffID = 1, Comment="Excellent", Rating1="P", Date=new DateTime(2010, 12, 31)},
new Rating(){StaffID = 1, Comment="Fair", Rating1="S", Date=new DateTime(2009, 12, 31)},
new Rating(){StaffID = 1, Comment="Poor", Rating1="U", Date=new DateTime(2008, 12, 31)},
new Rating(){StaffID = 2, Comment="Fair", Rating1="S", Date=new DateTime(2011, 1, 31)},
new Rating(){StaffID = 2, Comment="Excellent", Rating1="P", Date=new DateTime(2009, 12, 31)},
new Rating(){StaffID = 2, Comment="Poor", Rating1="U", Date=new DateTime(2008, 12, 31)},
new Rating(){StaffID = 3, Comment="Poor", Rating1="U", Date=new DateTime(2011, 2, 27)},
new Rating(){StaffID = 3, Comment="Excellent", Rating1="P", Date=new DateTime(2009, 12, 31)},
new Rating(){StaffID = 3, Comment="Fair", Rating1="S", Date=new DateTime(2008, 12, 31)},
};
CStaffDB StaffDb = new CStaffDB()
{
Staffs = lstStaff,
Ratings = lstRatings
};
var LatestEmployeeRatings =
(
from s in StaffDb.Staffs
join r in StaffDb.Ratings
on s.StaffID equals r.StaffID
select new
{
StaffID = s.StaffID,
Name = s.FirstName + ' ' + s.LastName,
Grade = s.CurrentGrade,
RatingDate = r.Date,
Rating1 = r.Rating1,
Comment = r.Comment
}
into ss
group ss by ss.StaffID into g
select g.OrderByDescending(r => r.RatingDate).First()
).ToList(); // <--easier to see in the debugger
LatestEmployeeRatings.ForEach(ler =>
{
Console.WriteLine("{0} {1} {2} {3} {4} {5}",
ler.StaffID, ler.Name, ler.Grade,
ler.RatingDate, ler.Rating1, ler.Comment);
});
}
}
}