我有这段代码可以从数据库获取数据
result = Context.APP_AuthorityHasamaForm.Where(x =>
x.UpdateTypeId == (int) UpdateType.Unit && x.AuthorityNum == authorityUnit.AuthorityNum &&
x.InsertDate >= authorityUnit.FromDate && x.HasamaFormStatus == (int) SendType.Meser &&
(x.APP_SignatureAuthorityHasamaForm.All(s =>
s.RoleId != (int) Role.EligibilityWorker1 && s.RoleId != (int) Role.DepartmentManager2 &&
s.RoleId != (int) Role.Treasurer3))).ToList();
这是工作。
现在,我想向其添加一个过滤器,以便仅出现“ APP_SignatureAuthorityHasamaForm”中的最新插入行。
该表中有“ SignatureDate”列,它指示插入日期。
我在这里找到了类似的问题:Similar qustion
答案 0 :(得分:0)
感谢@Flater评论,我明白了问题的重点。我真的不知道在恢复父记录时是否可以选择仅获取实体框架中记录的一部分(我认为不是)。但是,此替代方法可能对您有效:
result = Context.APP_AuthorityHasamaForm.Where(x =>
x.UpdateTypeId == (int) UpdateType.Unit && x.AuthorityNum == authorityUnit.AuthorityNum &&
x.InsertDate >= authorityUnit.FromDate && x.HasamaFormStatus == (int) SendType.Meser &&
(x.APP_SignatureAuthorityHasamaForm.All(s =>
s.RoleId != (int) Role.EligibilityWorker1 && s.RoleId != (int) Role.DepartmentManager2 &&
s.RoleId != (int) Role.Treasurer3)))
.Select(s=> new APP_AuthorityHasamaForm{
[Property] = s.[Property] //as many times as needed
APP_SignatureAuthorityHasamaForm = new List<APP_SignatureAuthorityHasamaForm> {s.APP_SignatureAuthorityHasamaForm.OrderByDescending(x => x.SignatureDate).FirstOrDefault()}
})
.ToList();