linq with max()和join

时间:2012-05-03 16:37:10

标签: linq

我正在使用的linq声明

            var renewalQuery = from r in crnnsupContext.Renewals
                               from w in crnnsupContext.WorkPlaces
                               join p in crnnsupContext.ProvStates on p.ProvinceStateID equals r.EmployerProvState
                               where  r.RegNumber == _username && r.PlaceOfWork == w.WorkID
                               select r;

我想要获得最新一年的r.RenewalYear,所以我尝试了类似

的内容
                var renewalQuery = from r in crnnsupContext.Renewals
                               group r by r.RenewalYear into g
                               from w in crnnsupContext.WorkPlaces
                               join p in crnnsupContext.ProvStates on p.ProvinceStateID equals r.EmployerProvState
                               where  r.RegNumber == _username && r.PlaceOfWork == w.WorkID
                               select g.max(r.RenewalYear);

显然它不起作用,有人知道这个的正确语法吗?非常感谢

1 个答案:

答案 0 :(得分:0)

选择您的年份,然后使用Max():

var renewalQuery = (from r in crnnsupContext.Renewals
                               group r by r.RenewalYear into g
                               from w in crnnsupContext.WorkPlaces
                               join p in crnnsupContext.ProvStates on p.ProvinceStateID equals r.EmployerProvState
                               where  r.RegNumber == _username && r.PlaceOfWork == w.WorkID
                               select r.RenewalYear).Max();