C# 如果第二个或第三个表为空,LINQ Join不返回结果
我有三张桌子:C# 如果第二个或第三个表为空,LINQ Join不返回结果,c#,asp.net,linq,join,C#,Asp.net,Linq,Join,我有三张桌子: Module_Articles_Articles Module_Articles_Categories Module_Articles_Comments 我想在repeater my query中显示我的文章: var articles = (from a in context.Module_Articles_Articles join c in context.Module_Articles_Categorie
Module_Articles_Articles
Module_Articles_Categories
Module_Articles_Comments
我想在repeater my query中显示我的文章:
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\u Articles\u Categories
或Module\u Articles\u Comments
为空时,我的查询不会返回任何结果!
我的代码是真的吗?如果没有,我该怎么做?您想要一个OUTTER连接,这可以通过添加
.DefaultIfEmpty()
您没有得到结果,因为LINQ连接导致
内部连接
s。您可能需要LEFT JOIN
s。按以下步骤做
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();
我想你需要在你的问题中添加
LINQ
标记,以获得快速答案。谢谢@sudhakartrapudi,补充。我将你的标题更新为问题,以前没有任何信息……非常感谢@ScottSelbystep通过代码,当断点到达这一行时,它将变成SQL语句(假设你使用SQL Server)然后直接在数据库中运行sql,看看它是否返回结果,或者实际发布生成的sql如何获取生成的sql语法?好的,实际上我刚刚更新了代码,出现了一些语法错误,现在试试,然后我将告诉您如何获取生成的sql
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();