2011-12-12 145 views
1

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

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

回答

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

感谢