在Linq

时间:2016-04-19 18:09:50

标签: vb.net linq

我是linq的新手并尝试将以下sql查询转换为linq;

  select c.ClientID , c.ClientName 
  , ISNULL((select 1 from clientcontactaccess cca 
        where cca.clientid = c.clientid and ClientContactID = 2141  ), 0) 'ClientAccess'
  from Clients c
  where c.GroupID = 1

到目前为止我已经尝试了这个但是不知道在哪里包含where子句;

 Dim query = (From c In db.Client Select New With _
               {Key .ClientID = c.ClientID, _
                 Key .ClientName = c.ClientName, _
                 Key .ClientAccess = (From cca In db.ClientContactAccess Where cca.ClientID = c.ClientID And cca.ClientContactID = _contactID)})

更新:

我已成功实现这一目标,但clientAccess字段未返回任何值。我需要返回1或0。

  Dim query = (From c In db.Client Where c.GroupID = 1 Select New With _
               {Key .ClientID = c.ClientID, _
                 Key .ClientName = c.ClientName, _
                 Key .ClientAccess = (From cca In db.ClientContactAccess Where cca.ClientID = c.ClientID And cca.ClientContactID = _contactID)})

1 个答案:

答案 0 :(得分:0)

我尝试转换您的SQL查询,但我不确定这对您有用,我认为在C#中将您的查询结果转换为这样的结果,您需要根据需要进行一些更改。

var result=from c in Clients select 
new {c.ClientID,c.ClientName, cca.Where(x=>x.ClientId=c.ClientID 
&& x.ClientContactId=2141).Any()} where c.GroupID==1;