2013-07-17 68 views
0

我已閱讀了其中的一些項目,包括How to select only the records with the highest date in LINQ,但我不知道如何將其應用於稍微複雜一點的案例。LINQ to SQL從對象獲取最新日期

我想獲得所有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' 
}) 
+0

你可以使用order by s.statusDate,並使用take(n)t o你需要兩個或任意數量的人。 –

+0

@VladimirBozic我想採取這種簡單的方法,但我認爲這將只需要1條記錄,而我想採取所有記錄(按日期分組,只採取最新的's.statusDate'日期)。 – user1477388

+0

我想要做這樣的事情,但我不知道如何應用它http://stackoverflow.com/questions/15367615/find-max-and-min-datetime-in-linq-group – user1477388

回答

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 
}) 
+0

這可能工作,但我希望爲此找到一個LINQ查詢。對於這樣的事情,Lambda有點冗長,但是謝謝。如果沒有其他人可以將這個或類似的東西翻譯成LINQ,我會接受。 – user1477388

+1

我很欣賞你的反饋。如果我成功了,我會嘗試一個嚴格的LINQ答案並將其作爲另一個答案發布。 – broguyman

+1

我做的編輯是我能想到的最好的LINQ答案。我希望它有幫助。 – broguyman