【问题标题】:linq 2 left joinslinq 2 左连接
【发布时间】:2011-12-12 16:09:38
【问题描述】:

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

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)或其他东西?

【问题讨论】:

    标签: linq entity-framework linq-to-entities


    【解决方案1】:

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

    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};
    

    【讨论】:

      【解决方案2】:

      你可以这样使用

        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) };
      

      谢谢

      【讨论】:

        猜你喜欢
        • 1970-01-01
        • 1970-01-01
        • 2017-04-10
        • 2011-08-10
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 2010-10-06
        • 1970-01-01
        相关资源
        最近更新 更多