2012-08-09 141 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