EF 复杂语句的使用

//EF多重排序
context.Serials
                      .Where(s => ("," + s.VideoGenreIds + ",").Contains("," + genreIds + ",") && s.IsEnable && !s.IsDel)
                      .OrderByDescending(v => v.Weight)
                      .ThenByDescending(v => v.Hot)
                      .Skip((currentPage - 1) * pageSize)
                      .Take(pageSize)
                      .Select(v => new SerialBriefModel
                      {
                          Id = v.Id,
                          SerialName = v.SerialName,
                          LastEpisode = v.LastEpisode,
                          Description = v.Description,
                          PicturePath = v.PicturePath,
                          AddTime = v.AddTime
                      }).ToList();



//EF group的使用以及 两表连接


var model = context.Serials
                     .Where(s => s.Id == serialId && s.IsEnable && !s.IsDel)
                     .GroupJoin(context.SerialEpisodes.Where(e=>!e.IsDel), s => s.Id, e => e.SerialId, (s, e) 
                     => new SerialDetailModel
                     {
                         Id = s.Id,
                         SerialName = s.SerialName,
                         VideoAreaId = s.VideoAreaId,
                         VideoAreaName = s.VideoArea.AreaName,
                         VideoGenreIds = s.VideoGenreIds,
                         VideoLanguageId = s.VideoLanguageId,
                         VideoLanguageName = s.VideoLanguage.Language,
                         TotalEpisodes = s.TotalEpisodes,
                         LastEpisode = s.LastEpisode,
                         Director = s.Director,
                         Starring = s.Starring,
                         ReleaseYear = s.ReleaseYear,
                         PicturePath = s.PicturePath,
                         Description = s.Description,
                         Fee = s.VideoChargeMethod.Fee,
                         Hot = s.Hot,
                         Score = s.Score,
                         SerialEpisodes = e.OrderBy(se => se.SerialIndex).Select(se => new SerialEpisodeModel
                         {
                             Id = se.Id,
                             SerialIndex = se.SerialIndex,
                             TagName = se.SerialTag.TagName,
                             Title = se.Title,
                             Description = se.Description
                         }
                         ).ToList()
                     }).FirstOrDefault();

 

posted @ 2016-03-03 15:07  大空白纸  阅读(429)  评论(0编辑  收藏  举报