Warning: file_get_contents(/data/phpspider/zhask/data//catemap/6/entity-framework/4.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
C# 使用EF从外部表中仅获取最新的插入行_C#_Entity Framework - Fatal编程技术网

C# 使用EF从外部表中仅获取最新的插入行

C# 使用EF从外部表中仅获取最新的插入行,c#,entity-framework,C#,Entity Framework,我有这个代码从数据库中获取数据 result = Context.APP_AuthorityHasamaForm.Where(x => x.UpdateTypeId == (int) UpdateType.Unit && x.AuthorityNum == authorityUnit.AuthorityNum && x.InsertDate >= authorityUnit.FromDa

我有这个代码从数据库中获取数据

 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\u signatureauthority hasamaform”中的最新插入行。
我在该表中有一列“SignatureDate”,表示插入日期


我在这里发现了一个类似的问题:

多亏了@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();
result=Context.APP\u AuthorityHasamaForm.Where(x=>
x、 UpdateType ID==(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)角色(3)))
.选择(s=>new APP_AuthorityHasamaForm{
[Property]=s[Property]//需要多少次就多少次
APP\u SignatureAuthorityHasamaForm=新列表{s.APP\u SignatureAuthorityHasamaForm.OrderByDescending(x=>x.SignatureDate).FirstOrDefault()}
})
.ToList();

为什么Jon Skeet提供的关于该问题的解决方案对您无效?@Rumpelstinsk:大概是因为Jon Skeet的答案只获取历史记录条目,而OP想要父实体(APP_AuthorityHasamaForm)及其最新历史记录条目(APP_SignatureAuthorityHasamaForm)。这确实改变了语法,它不像Jon的回答那么简单/优雅。我不知道“OrderByDescending”放在哪里以及如何放置。如果我把它放在“Where”之后,那么我只需要过滤父表,我需要过滤子表,因为已经有一些误解,问题的措辞让它有点难以识别,OP想要“每个权限及其最近的签名,而不是每个权限及其所有签名”。到目前为止,大多数人似乎都在回答如何获取“最新签名”,这与获取授权+签名的组合明显不同。您所说的“[Property]=s.[Property]//根据需要多次”是什么意思?我需要放置什么来代替“属性”?简化对象的所有属性,
UpdateTypeId=s.UpdateTypeId,…
,等等。智能感知将帮助你,