Entity Framework - 左连接中的最新记录
Entity Framework - Most recent record in the left join
我知道周围有很多类似的查询 post,但我想知道我们如何在 entity framework、[=12= 中编写下面的 SQL 查询]
select * from RequestDetail d
left join (select RequestDetailId, Max(RequestedOn) RequestedOn from RequestHistory group by RequestDetailId) as h
on h.RequestDetailId = d.Id
读了很多 post 但我找不到精确的副本。
您可以在 Linq to Entities 中执行相同的操作:
var innerquery=from e in RequestHistory
group e by e.RequestDetailId into g
select new {
RequestDetailId=g.Key,
RequestedOn =g.Max(r=>r.RequestedOn)
};
var query= from d in RequestDetail
join h in innerquery on d.Id equals h.RequestDetailId into gj
from e in gj.DefaultIfEmpty()
select new {d, e};
我首先创建了内部查询以帮助您更好地理解如何执行此操作,但是您可以将两个查询合并为一个,但这没有任何区别。
我知道周围有很多类似的查询 post,但我想知道我们如何在 entity framework、[=12= 中编写下面的 SQL 查询]
select * from RequestDetail d
left join (select RequestDetailId, Max(RequestedOn) RequestedOn from RequestHistory group by RequestDetailId) as h
on h.RequestDetailId = d.Id
读了很多 post 但我找不到精确的副本。
您可以在 Linq to Entities 中执行相同的操作:
var innerquery=from e in RequestHistory
group e by e.RequestDetailId into g
select new {
RequestDetailId=g.Key,
RequestedOn =g.Max(r=>r.RequestedOn)
};
var query= from d in RequestDetail
join h in innerquery on d.Id equals h.RequestDetailId into gj
from e in gj.DefaultIfEmpty()
select new {d, e};
我首先创建了内部查询以帮助您更好地理解如何执行此操作,但是您可以将两个查询合并为一个,但这没有任何区别。