如果第二个或第三个表为空,则LINQ Join不返回结果

时间:2013-12-07 00:59:25

标签: c# asp.net linq join

我有3张桌子:

Module_Articles_Articles
Module_Articles_Categories
Module_Articles_Comments

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

var articles =
                (from a in context.Module_Articles_Articles
                 join c in context.Module_Articles_Categories on a.CategoryID equals c.CategoryID
                 join co in context.Module_Articles_Comments on a.ArticleID equals co.ArticleID
                 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,
                     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();

但是当Module_Articles_CategoriesModule_Articles_Comments为空时,我的查询不会返回任何内容! 我的代码是真的吗?如果不是我怎么能这样做?

2 个答案:

答案 0 :(得分:7)

你想要一个OUTER JOIN,只需添加.DefaultIfEmpty()

就可以在这样的查询中完成
from a in context.Module_Articles_Articles
join c in context.Module_Articles_Categories on a.CategoryID equals c.CategoryID into ca
from c in cs.DefaultIfEmpty()
join co in context.Module_Articles_Comments on a.ArticleID equals co.ArticleID into com
from co in com.DefaultIfEmpty()
where a.IsDraft == false
orderby a.ArticleID descending
select new ...

答案 1 :(得分:3)

您没有得到结果,因为您的LINQ加入导致INNER JOIN s。你可能想要LEFT JOIN。这样做。

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,
         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();