2011-12-21 140 views
3

FollowingUsers如何編寫LINQ查詢?

StatusUpdates

上面可以看到FollowingUsersStatusUpdates表。

FollowingUsers,我存儲FollowerUsernameFollowingUsername。 在StatusUpdates,我店Status updates的用戶。

下面你可以看到我寫的檢索誰在登錄的用戶的狀態更新原始查詢。

var list = new List<StatusUpdate>(); 
list = (from x in db.StatusUpdates 
     where x.Author == User.Identity.Name 
     orderby x.Timestamp descending 
     select x) 
     .Take(count).ToList(); 

如何從以下獲取狀態更新的登錄用戶?

回答

2

以下應該工作,雖然我沒有你的數據庫來測試它。請注意,在調用ToList之前它不會被執行,因此所有事情都應該在單個數據庫查詢中發生。此外,不需要創建新列表,因爲它將被您的查詢覆蓋,所以我已經整理了一下。

var Users = from f in db.FollowingUsers 
      where f.FollowerId == User.Identity.Name 
      select f.FollowingId; 

var list = (from x in db.StatusUpdates 
      from y in Users 
      where x.Author == y 
      orderby x.Timestamp descending 
      select x) 
      .Take(count).ToList(); 
+0

謝謝Tuskan360。但它沒有工作:( – ChamingaD 2011-12-21 18:36:21

+2

你能更具體嗎?什麼沒有工作?你得到一個錯誤信息嗎?它編譯嗎?你剛剛得到錯誤的數據嗎? – ForbesLindesay 2011-12-21 19:14:04

+0

另外,你是否使用查詢SQL數據庫LINQ to SQL?或其他東西? – ForbesLindesay 2011-12-21 19:14:37