linq外连接和分组

时间:2016-08-02 10:46:06

标签: linq group-by outer-join

我对linq很新,并尝试将内部联接转换为外部联接查询,但无法使其工作。

当前查询如下所示 -

(from learnerRating in DbContext.LearnerRatings
  join rating in DbContext.Ratings on learnerRating.RatingId equals rating.Id
  join lesson in DbContext.Lessons on learnerRating.LessonId equals lesson.Id
  join user in DbContext.Users on lesson.UserId equals user.UserId
  where user.CentreId == centreid
  group rating by rating.Name into sectiongroup
  orderby sectiongroup.Count() descending
  select new ChartPointsViewModel
  {
        ValueStr = sectiongroup.Key,
        Value = sectiongroup.Count(),
        order = sectiongroup.Max(r => r.Id)
 }).OrderBy(rc => rc.order);

但是,如果没有学习,那么我不会得到评级。所以我想在评级和学习者评分上做左外连接,但这是我所有路径都失败的地方。我重新排序以进行外连接

from rating in DbContext.Ratings
     join learnerRating in DbContext.LearnerRatings on rating.Id equals learnerRating.RatingId into learnratingsjoininner

但是,我如何加入其他数据以过滤掉中心的用户,然后分组?

我可以在没有过滤器的情况下运行查询 -

(from rating in DbContext.Ratings
     join learnerRating in DbContext.LearnerRatings on rating.Id equals learnerRating.RatingId into learnratingsjoininner                                
     from lrinner in learnratingsjoininner.DefaultIfEmpty()
     //where learnerRating.Lesson.User.CentreId == centreid
    group rating by rating.Name into sectiongroup

    select new ChartPointsViewModel
    {
           ValueStr = sectiongroup.Key,
           Value = sectiongroup.Count(),
           order = sectiongroup.Max(r => r.Id)
    }).OrderBy(rc => rc.order);

但是我如何添加过滤器

where learnerRating.Lesson.User.CentreId == centreid

0 个答案:

没有答案