linq 2离开了加盟

时间:2011-12-12 16:09:38

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

所以我想从我的左连接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)或什么?

2 个答案:

答案 0 :(得分:4)

这应该可以为您提供所需的内容

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

答案 1 :(得分:0)

你可以这样使用

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

由于