我有一个名为public class StudentEntity : INotifyPropertyChanged
{
private string studentId;
public string StudentId
{
get { return studentId; }
set
{
studentId = value;
OnPropertyChanged("StudentId");
}
}
private string studentStatus;
public string StudentStatus
{
get { return studentStatus; }
set
{
studentStatus = value;
OnPropertyChanged("StudentStatus");
}
}
public string getStatusChangeDate { get; set; }
public StudentEntity(string studentId, string studentStatus)
{
StudentId = studentId;
StudentStatus = studentStatus;
}
public event PropertyChangedEventHandler PropertyChanged;
protected void OnPropertyChanged(string name)
{
PropertyChanged?.Invoke(this, new PropertyChangedEventArgs(name));
}
}
class Program
{
static void Main(string[] args)
{
var person = new StudentEntity("101", "Accept");
person.PropertyChanged += Person_PropertyChanged;
person.StudentStatus = "Reject";
Console.ReadLine();
}
private static void Person_PropertyChanged(object sender, PropertyChangedEventArgs e)
{
StudentEntity studentEntity = sender as StudentEntity;
if (e.PropertyName == "StudentStatus")
{
studentEntity.getStatusChangeDate = DateTime.Now.ToString();
}
}
}
的表,我想为每个员工检索每个合约的最高EmployementContracts
,包括来自链接/相关表的其他一些属性。我试着吼道:
StartDate
我只能从中获取_dbContext.EmployementContracts
.Include(x => x.Employee)
.Include(x => x.Department)
.Where(x => x.SupervisorId == 1)
.GroupBy(c => c.EmployeeId)
.Select(g => new
{
StartDate = g.Max(x => x.StartDate)
}).ToList();
列(每条记录的最高日期),但如何从包含的表中获取其他一些属性?
例如我需要:
来自StartDate
的{{1}}和来自EmployeeName
表的Employee
。
答案 0 :(得分:0)
您的选择语句是您将获得的结果。您的结果只是一个包含一个字段StartDate
的匿名对象。您可以在那里调用.Select()
将整个搜索以及表和数据返回给匿名对象。
答案 1 :(得分:0)
我建议您使用以下代码:
_dbContext.EmployeeContracts
.Where(x => x.SuperVisorId == 1)
.GroupBy(x => x.EmployeeId)
.Select(x=>x.OrderByDescending(y=>y.StartDate).FirstOrDefault())
.Include(x => x.Employee)
.Include(x => x.Department)
.ToList();
技巧是唯一从分组中检索相关记录。然后,每组中的每条记录都是您想要查看的employeeContract。另请注意,.Include()语句将移至后面。这是因为分组会更改Queryable结构并删除包含的导航属性,因此一旦您拥有所需的结构,就必须包含。