2
select C.CenterID
from   dbo.Center C 
 inner join (select PersonID, max(EffectiveDate) as EffectiveDate
         from   Center
         where  EffectiveDate <= getdate()
         group by PersonID) as C2 
 on C.PersonID= C2.PersonID
  and C.EffectiveDate = C2.EffectiveDate

中心表有一个 PersonID 和 EffectiveDate,多条记录具有相同的 PersonID,但不同的 EffectiveDates,我试图为每个 PersonID 返回 1 个最新记录

理想情况下,我想在 linq 中将其表示为 IQueryable,以便我可以使用它来构建更大的查询。

4

1 回答 1

2
var q = from c in oc.Center
join c2 in (
  from ci in oc.Center
  where ci.EffectiveDate <= DateTime.Now
  group ci by ci.PersonID into cig
  select new { PersonID = cig.Key, EffectiveDate = cig.Max(ed => ed.EffectiveDate) }
 ) on new { c.PersonID, c.EffectiveDate } equals { c2.PersonID, c2.EffectiveDate }
select c.CenterID
于 2011-01-21T02:30:11.713 回答