左连接和条件Linq到SQL

时间:2016-04-22 11:59:54

标签: c# linq

如何将以下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.ClientContactID = 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);

1 个答案:

答案 0 :(得分:0)

我最终使用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的新手。