2012-08-09 143 views
1

我怎样才能做到这一点的SQL查询在LINQ多个参数:LINQ - 左外连接在Where子句

select * from chat c 
left outer join lead s on c.key = s.id 
where (typeId = 5 AND c.key = @clientId) OR (c.typeId = 4 AND s.clientId = @clientId) 

还是这个SQL查询 - 相同,相同的

select * from chat c 
where (typeId = 5 AND c.key = @clientId) OR (typeId = 4 AND c.key in (select id from lead where clientId = @clientId)) 

我有什么:

var chatter = (from chat in linq.Chat 
      join lead in linq.Lead 
       on chat.key equals lead.Id.ToString() into clientLeads 
       from cl in clientLeads.Where(l => l.clientId == clientId).DefaultIfEmpty() 
      where (chat.typeId == 5 && chat.key == clientId.ToString()) || 
       (chat.typeId == 4 && chat.key == cl.Id.ToString()) 
       select chat).WithPath(prefetchPath).OrderByDescending(c => c.CreatedDate); 

上面的LINQ查询不会从WHERE子句中获得任何结果,我错过了什么?

回答

1

我翻译的第二查询LINQ:

var leadIds = linq.Lead.Where(l => l.clientId == clientId.ToString()).Select(l => l.id); 
var chatter = from chat in linq.Chat 
       where (chat.typeId == 5 && chat.key == clientId.ToString()) || 
        (chat.typeId == 4 && leadIds.Contains(chat.key)); 
+0

人,其实我试过的东西很接近,但我一定是搞砸了!这工作! – MartinHN 2012-08-09 11:26:07