3 回答
TA贡献1856条经验 获得超5个赞
这与您的查询等效。
var res = from s in PhoneNumberServiceItems
join p in PhoneNumbers on s.PhoneNumber_Id equals p.Id
join tm in ( from p1 in PhoneNumberServiceItems
group p1 by p1.PhoneNumber_Id into p_g
select new {PhoneNumber_Id = p_g.Key,MaxDate = p_g.Max(i=> i.Created) } )
on new {Created = s.Created, PhoneNumber_Id = s.PhoneNumber_Id}
equals new { Created = tm.MaxDate, PhoneNumber_Id = tm.PhoneNumber_Id}
where p.NumberRangeId == {Id}
select new
{
s.PhoneNumber_Id,
s.State,
s.Created
};
TA贡献1884条经验 获得超4个赞
尝试以下更简单的方法:
var res = (from nums in _db.PhoneNumbers.Where(x => NumberRangeId == id) join serviceItems in _db.PhoneNumberServiceItems on nums.PhoneNumber_Id equals serviceItems.Id select new {serviceItems = serviceItems, nums = nums}) .OrderByDescending(x => x.serviceItems.Created) .GroupBy(x => x.nums.PhoneNumber_Id) .Select(x => x.First()) .Select(x => new {Id = x.nums.PhoneNumber_Id, state = x.serviceItems.State, maxDate = x.serviceItems.Created}) .ToList();
TA贡献1809条经验 获得超8个赞
var recentPhoneNos= from psi in _db.PhoneNumberServiceItems
group psi by psi .PhoneNumber_Id into psiTemp
select new {
PhoneNumber_Id = psiTemp.Key,
MaxDate = psiTemp.Max(i=> i.Created)
};
var res=from serviceItems in _db.PhoneNumberServiceItems
join nums in _db.PhoneNumbers on serviceItems.PhoneNumber_Id equals nums.Id
join serviceGroup in recentPhoneNos on nums.Id equals serviceGroup .PhoneNumber_Id
where nums.NumberRangeId == id && serviceGroup.MaxDate
select new {
State = serviceItems.State,
NumId = serviceGroup.NumId,
Created = serviceGroup.MaxDate
} ;
- 3 回答
- 0 关注
- 102 浏览
添加回答
举报