左连接与和条件 Linq to SQL

本文关键字:Linq to SQL 条件 连接 | 更新日期: 2024-11-06 01:30:16

如何将以下sql查询转换为linq到sql?

select c.ClientID , c.ClientName , case when cca.clientID >0  then 1 else 0 end 'ClientAccess'  from Clients c
left join clientcontactaccess cca ON cca.clientid = c.ClientID and cca.ClientContactID = 2141
where c.GroupID = 1

到目前为止,我已经这样做了,但不知道如何处理"和 cca。客户端联系人 ID = 2141" 条件;

dynamic query = (from c in db.Clientdb.ClientContactAccesscca.ClientIDc.ClientIDGroupfrom cca in GroupDetails.DefaultIfEmpty()where c.GroupID == 1c.ClientIDc.ClientNamecca.ClientID.ToString == null ? 0 : 1);

左连接与和条件 Linq to SQL

我最终使用了带有join的where子句(第二行)

Dim query = (From c In db.Client _
                 Group Join cca In db.ClientContactAccess.Where(Function(cca) cca.ClientContactID = _contactID) On cca.ClientID Equals c.ClientID _
                 Into GroupDetails = Group _
                 From cca In GroupDetails.DefaultIfEmpty() Where c.GroupID = 1
                 Select c.ClientID, c.ClientName, ClientAccess = If(cca.ClientID.ToString Is Nothing, 0, 1))

如果有人有更好的解决方案,那么请发帖,因为这会增加知识,而且我是linq的新手。