LINQ to SQL从Object获取最新日期

时间:2013-07-17 13:44:00

标签: c# asp.net-mvc-4 linq-to-sql

我已经阅读了一些相关内容,包括How to select only the records with the highest date in LINQ,但我不知道如何将它应用到我的案例中,这种情况稍微复杂一些。

我正在尝试获取所有AdjusterProfileStatusItem,但只选择最新的s.statusDate。目前,查询只返回所有记录的所有日期;而我只想要所有记录的最新日期。

(from u in db.Users
join a in db.Adjusters
on u.id equals a.userID
join s in db.AdminAdjusterStatus
on a.id equals s.adjusterID
where u.userType.ToLower() == "adjuster"
&& s.status.ToLower() == "approved"

&& s.statusDate.Max() // causes syntax error...

select new AdjusterProfileStatusItem
{
   user = u,
   adjuster = a
})

修改

我也试过这个给我一个语法错误......

(from u in db.Users
join a in db.Adjusters
on u.id equals a.userID
join s in db.AdminAdjusterStatus
on a.id equals s.adjusterID
where u.userType.ToLower() == "adjuster"
&& s.status.ToLower() == "approved"
group new { u, a, s } by s.adjusterID into x
select new AdjusterProfileStatusItem
{
    user = u, // u does not exist in context
    adjuster = a, // a does not exist in context
    status = x.Max(y => y.statusDate) // anonymous type does not contain definition for 'statusDate'
})

1 个答案:

答案 0 :(得分:1)

我不确定你对Lambda表达的看法,但我可能会这样做:

db.Users
  .Join(db.Adjusters,
  u => u.Id,
  a => a.UserId,
  (u, a) => new 
  {
       User = u,
       Adjuster = a
  })
  .Join(db.AdminAdjusterStatus,
  a => a.Adjuster.Id,
  s => s.AdjusterId,
  (a, s) => new 
  {
       User = a.User,
       Adjuster = a.Adjuster,
       AdminAdjusterStatus = s
  })
  .Where(x => x.User.userType == "adjuster"
      && x.AdminAdjusterStatus.status == "approved"
      && x.AdminAdjusterStatus.statusDate == db.AdminAdjusterStatus
                             .Where(y => y.AdjusterId == 
                                    x.AdminAdjusterStatus.AdjusterId)
                             .Max(z => z.statusDate))
  .Select(a => new AdjusterProfileStatusItem
  {
      user = a.User
      adjuster = a.Adjuster
  })

**EDIT!!!**


(from u in db.Users
join a in db.Adjusters
on u.id equals a.userID
join s in db.AdminAdjusterStatus
on a.id equals s.adjusterID
where u.userType.ToLower() == "adjuster"
&& s.status.ToLower() == "approved"

&& s.statusDate == GetMaxStatusDate(db.AdminAdjusterStatus.ToList(), s.AdjusterID)

select new AdjusterProfileStatusItem
{
   user = u,
   adjuster = a
})

private DateTime GetMaxStatusDate(List<AdminAdjusterStatus> statuses, int adjusterId)
{
     return (from a in statuses
             where a.AdjusterId == adjusterId
             group a by a.AdjusterId into values
             select values.Max(x => x.statusDate)).FirstOrDefault();
}



 OR 



(from u in db.Users
join a in db.Adjusters
on u.id equals a.userID
join s in db.AdminAdjusterStatus
on a.id equals s.adjusterID
where u.userType.ToLower() == "adjuster"
&& s.status.ToLower() == "approved"

&& s.statusDate == db.AdminAdjusterStatus
                   .Where(x => x.AdjusterId == s.AdjusterId)
                   .Select(y => y.statusDate)
                   .Max();

select new AdjusterProfileStatusItem
{
   user = u,
   adjuster = a
})