外部联接不能按我的意愿工作

时间:2013-12-23 12:02:24

标签: c# asp.net sql linq

我有3张桌子:

  • Module_Articles_Articles
  • Module_Articles_Categories
  • Module_Articles_Comments

我希望通过此查询在转发器中显示我的文章:

var context = new IPArticlesEntities();
var articles =
                (from a in context.Module_Articles_Articles
                 join c in context.Module_Articles_Categories on a.CategoryID equals c.CategoryID into joinTable1
                 from c in joinTable1.DefaultIfEmpty()
                 join co in context.Module_Articles_Comments on a.ArticleID equals co.ArticleID into joinTable2
                 from co in joinTable2.DefaultIfEmpty()
                 where a.IsDraft == false
                 orderby a.ArticleID descending
                 select new
                 {
                     a.ArticleID,
                     a.ArticleTitle,
                     a.ArticleContent,
                     a.Image,
                     a.Sender,
                     a.SentDate,
                     a.Summary,
                     a.Likes,
                     a.Dislikes,
                     a.Tags,
                     a.PostMode,
                     a.ViewCount,
                     c.CategoryID,
                     c.CategoryTitle,
                     AcceptedCommentsCount = 
                     (from com in context.Module_Articles_Comments where com.ArticleID == a.ArticleID && com.Status select com)
                     .Count(),
                     DeniedCommentsCount =
                     (from com in context.Module_Articles_Comments where com.ArticleID == a.ArticleID 
                          && com.Status == false select com)
                     .Count()
                 }).ToList();
            return articles;

当我在Comments表中没有数据时,它工作正常,但如果我在comments表中有3条记录,它将返回一条文章记录3次!

我的意思是我有一篇文章记录和3条评论,当我运行此查询时,它返回3条相同的文章记录。

如何解决这个问题?

0 个答案:

没有答案