从linq到实体

时间:2013-07-04 11:50:51

标签: asp.net-mvc linq-to-entities

   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来命令“结果”,例如:看到它的orderby可能需要movieID 2,25, 7,14,25然后我按照与看见者相同的顺序需要AllMovieInfo。如何根据“seenitorderby”订购“结果”?

1 个答案:

答案 0 :(得分: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));