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”对结果进行排序,这很明显,但我必须根据“seenitorderby”的 id 对“结果”进行排序,例如:看到它 orderby 可能需要 movieID 2、25、7、14 ,25 那么我需要 AllMovieInfo 按照与 seeitorderby 相同的顺序。如何根据“seenitorderby”订购“结果”?

4

1 回答 1

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));
于 2013-07-04T12:00:07.530 回答