gpt4 book ai didi

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

转载 作者:太空狗 更新时间:2023-10-30 00:19:27 26 4
gpt4 key购买 nike

我有三个表:

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 为空时,我的查询什么都不返回!我的代码是真的吗?如果没有,我该怎么做?

最佳答案

你想要一个外部连接,这可以通过简单地添加 .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 ...

关于c# - 如果第二个或第三个表为空,LINQ Join 不返回结果,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/20436212/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com