我对LINQ to SQL很新,并尝试在LINQ中编写以下查询。有人可以指导我如何在LINQ中编写它。
Select app.ApplicationGenID, max(appHis.ApplicationStatusHistoryGenID), max (appHis.ApplicationStatusGenID)
from Application app
join Applicant appl on app.ApplicantGenId = appl.ApplicantGenID
Left Outer Join ApplicationStatus appStatus on app.ApplicationGenID = appStatus.ApplicationGenID
Where appl.ClientID=4 and (app.IsDeleted=0 or app.IsDeleted is null)
Group By app.ApplicationGenID
答案 0 :(得分:0)
我只是没有从您的SQL查询中看到您在哪里获得appHis
表。
但是,如果您的表上有密钥,扩展方法语法中的 LinQ 应该如下所示:
var res = _context.Applicant
.Where(x => x.ClientID == 4
&& (x.Application.IsDeleted == 0 || x.Application.IsDeleted == null))
.GroupBy(x => x.ApplicationGenID)
.Select(x => new
{
ApplicationGenID = x.Key,
//Not sure about next 2 lines, couse i don't know where you get appHis
ApplicationStatusHistoryGenID = x.Max(y => y.ApplicationStatusHistoryGenID),
ApplicationStatusGenID = x.Max(y => y.ApplicationStatusGenID)
});
答案 1 :(得分:0)
谢谢你的帮助。这就是我最终做到的方式。把它放在这里供将来参考。
var result = (from app in context.Application
join appl in context.Applicant on new {ApplicantGenId = app.ApplicantGenId} equals
new {ApplicantGenId = appl.ApplicantGenID}
join appStatus in context.ApplicationStatus on app.ApplicationGenID equals
appStatus.ApplicationGenID into appStatus_join
from appStatus in appStatus_join.DefaultIfEmpty()
join refStatus in context.ref_ApplicationStatus on appStatus.ApplicationStatusGenID
equals refStatus.ApplicationStatusGenID into refStatus_join
from refStatus in refStatus_join.DefaultIfEmpty()
where
appl.ClientID == 4 &&
(app.IsDeleted == false ||
app.IsDeleted == null)
group new {app, appStatus, refStatus} by new
{
app.ApplicationGenID
}
into g
select new
{
ApplicationGenID = (int?) g.Key.ApplicationGenID,
ApplicationStatusGenID = (int?) g.Max(p => p.appStatus.ApplicationStatusGenID),
ApplicationStatusDesc = g.Min(p => p.refStatus.ApplicationStatusDesc),
ApplicationStatusDate = (DateTime?) g.Max(p => p.appStatus.StatusDate)
}).ToList();