2013-07-04 80 views
1
AllMovieInfo = from movieInfo in AllMovieInfo 
          from countryMovie in movieInfo.SeenItWantToSeenIt 
          where countryMovie.Status==1 
          select movieInfo; 

      var seenitorderby = db.SeenItWantToSeeIt.Where(m => m.Status == 1).GroupBy(m => m.MovieID).Select(g => new {MovieID =g.Key,count=g.Count()}).OrderBy(o=>o.count); 
      List<int> seenItList=seenitorderby.Select(s=>s.MovieID).ToList(); 
      AllMovieInfo = (from a in AllMovieInfo 
         from s in seenItList 
         where seenItList.Contains(a.MovieID) 
         select a).Distinct(); 

該查詢是根據「AllMovieInfo.MovieID」,這是顯而易見的排序結果,但我必須爲了根據隨附的ID的「結果」是「seenitorderby」,例如:看到它orderby可能需要movieID 2,25,7,14,25,那麼我需要按照與參觀者相同的順序使用AllMovieInfo。如何根據「seesitorderby」命令「結果」?排序依據的LINQ到實體

+0

根據你的連接是不是'AllInfo.ID'和'SeenInfo.ID'一樣? – NinjaNye

+0

那麼您希望訂購的「SeenInfo」主鍵是什麼? – NinjaNye

+0

請注意,它們現在被'Id'命令返回的事實是純粹的機會(最可能是數據庫實現的方式)。除非您明確提供了訂單,否則假定結果將是隨機的,即使它們似乎不在您的開發系統中 – Basic

回答

0

根據你的連接是不是AllInfo.ID和SeenInfo.ID一樣?

如果我錯了以下應該這樣做

var result= (from a in AllInfo 
      from s in SeenInfo 
      where s.ID==a.ID 
      orderby s.ID // <-- this should be the SeenInfo primary key 
      select a).Distinct(); 

更新:根據問題的更新

感謝您的更新。我想我現在明白你的問題。你希望通過計數特定電影訂購...

AllMovieInfo = from movieInfo in AllMovieInfo 
       from countryMovie in movieInfo.SeenItWantToSeenIt 
       where countryMovie.Status==1 
       select movieInfo; 

var seenItOrderBy = db.SeenItWantToSeeIt 
         .Where(m => m.Status == 1) 
         .GroupBy(m => m.MovieID) 
         .Select(g => new { MovieID = g.Key, Count=g.Count()}); 

var result = (from a in AllMovieInfo 
       from s in seenItOrderBy 
       where s.MovieID = a.ID 
       orderby s.Count 
       select a).Distinct(); 

您可以簡化這個如下...

請注意:這是從我的頭頂並基於你相信你正試圖在你的代碼中實現,所以請採取這樣做。

var result = db.AllMovieInfo 
       // Where someone wants to see it wants to see it 
       .Where(mi => mi.SeenItWantToSeeIt.Any(m => m.Status == 1)) 
       // Order by number of people that have seen or want to see it 
       .OrderBy(mi => mi.SeenItWantToSeeIt.Count(m => m.Status == 1)); 
+0

仍然不能正常工作.....我的完整代碼是 –

+0

這會給出與您現有代碼相同的結果,直到您將'orderby'行更改爲'SeenInfo'主鍵。由於'SeenInfo.ID'是'AllInfo'的外鍵,所以它們與我編輯過的問題一樣 – NinjaNye

+0

。請看看它。 –