linq 2 左连接

发布于 2024-12-20 14:17:46 字数 1209 浏览 3 评论 0原文

所以我想从我的左连接 sql 中进行 linq 查询(请参阅下面)。我只是不知道如何在连接上正确定位“.TournamentId = 1”条件。目前,当在我的数据库上运行它时,我得到了我想要的结果。这是类型表中带有空字段的几行。

select typ.Id, stat.PromoterId, temp.PromoterId
from ReportTypes type
left join ReportTemplateStatus status on status.PromoterId = type.TypeId and status.TournamentId = 1
left join ReportTemplates temp on temp.ClientId = status.PromoterId and temp.TournamentId = 1

Promoter
 - promoterId
 - promoterName

Tournament
 - tournamentId
 - tournamentName

ReportType
 - TypeId

ReportTemplateStatus 
 - promoterId (this is the key)
 - tournamentId
 - typeId

ReportTemplates
 - promoterId
 - tournamentId

这就是我目前所拥有的:

var report  =  from type in context.ReportTypes 
                 join status in context.ReportTemplateStatus on type.TypeId equals status.TypeId
                 join temp in context.ReportTemplates  on status.promoterId equals temp.promoterId into iReports
                 from reports in iReports.Where (rep => rep.promoterId == _promoterId && rep.tournamentId == _tournamentId).DefaultIfEmpty()
select new { my fields});

但它给了我一个空值。

关于 linq 应该如何工作有什么想法吗?也许分成“itables”(iReports)或其他东西?

So I wanted to make a linq query out of my left join sql (refer to it below). I just don't know how to properly position the ".TournamentId = 1" condition on the joins. Currently when running this on my database I get the results that I want. which is a couple of rows from the Type table with null fields.

select typ.Id, stat.PromoterId, temp.PromoterId
from ReportTypes type
left join ReportTemplateStatus status on status.PromoterId = type.TypeId and status.TournamentId = 1
left join ReportTemplates temp on temp.ClientId = status.PromoterId and temp.TournamentId = 1

Promoter
 - promoterId
 - promoterName

Tournament
 - tournamentId
 - tournamentName

ReportType
 - TypeId

ReportTemplateStatus 
 - promoterId (this is the key)
 - tournamentId
 - typeId

ReportTemplates
 - promoterId
 - tournamentId

This is currently what I have:

var report  =  from type in context.ReportTypes 
                 join status in context.ReportTemplateStatus on type.TypeId equals status.TypeId
                 join temp in context.ReportTemplates  on status.promoterId equals temp.promoterId into iReports
                 from reports in iReports.Where (rep => rep.promoterId == _promoterId && rep.tournamentId == _tournamentId).DefaultIfEmpty()
select new { my fields});

but it's giving me a null.

any ideas on how the linq should work? maybe separate into "itables" (iReports) or something?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

浴红衣 2024-12-27 14:17:46

这应该会给你你正在寻找的东西

var report  =  from type in context.ReportTypes 
               from status in context.ReportTemplateStatus.Where(x => type.TypeId == x.TypeId)        
                                                          .Where(x => x.TournamentId == 1)  
                                                          .DefaultIfEmpty()
               from reports in context.ReportTemplates.Where(x => status.promoterId == x.promoterId)
                                                      .Where(x => x.TournamentId == 1)
                                                      .DefaultIfEmpty()
               select new { my fields};

This should give you what you are looking for

var report  =  from type in context.ReportTypes 
               from status in context.ReportTemplateStatus.Where(x => type.TypeId == x.TypeId)        
                                                          .Where(x => x.TournamentId == 1)  
                                                          .DefaultIfEmpty()
               from reports in context.ReportTemplates.Where(x => status.promoterId == x.promoterId)
                                                      .Where(x => x.TournamentId == 1)
                                                      .DefaultIfEmpty()
               select new { my fields};
永不分离 2024-12-27 14:17:46

你可以像这样使用

  var query = from person in people
                    join pet in pets on person equals pet.Owner into gj
                    from subpet in gj.DefaultIfEmpty()
                    select new { person.FirstName, PetName = (subpet == null ? String.Empty : subpet.Name) };

谢谢

You can use like this

  var query = from person in people
                    join pet in pets on person equals pet.Owner into gj
                    from subpet in gj.DefaultIfEmpty()
                    select new { person.FirstName, PetName = (subpet == null ? String.Empty : subpet.Name) };

Thanks

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文