1

如何在 LINQ 中执行此 SQL 查询:

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 子句中产生任何结果,我错过了什么?

4

1 回答 1

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));
于 2012-08-09T11:15:08.840 回答