2014-05-23 47 views
0

我需要獲取與給定業務和給定聯繫碼相關的聯繫。聯繫人可以有多個聯繫人代碼。在fk表上有多個聯接的LINQ查詢

這是接近我已經能夠得到 - 任何幫助表示讚賞...

int bid = 10; 

    var sendto = from businesscontact in db.businesscontacts.Where(bus => bus.businessid == bid).Include(bc => bc.contact where contact.contactscontactcodes.contactcodesid.Contains(55)) select r.email; 

這相當於T-SQL我之後...

SELECT contacts.email 
    FROM businesscontacts 
    INNER JOIN contacts ON businesscontacts.contactsid = contacts.contactsid 
    INNER JOIN contactscontactcodes ON contacts.contactsid = contactscontactcodes.contactsid 
where contactscontactcodes.contactcodesid = @cid 
AND businesscontacts.businessid = @bid 

回答

1
using(EntityClass entities = new EntityClass()) 
{ 
var email = 
     (from biz in entities.BusinessContacts 
     where biz.businessid = bid 
     from codes in entities.ContactsContactCodes 
     where codes.contactcodesid = cid 
     from c in entites.Contacts 
     where c.contactsid == codes.contactsid && c.contactsid == biz.contactsid 
     select c.email).FirstOrDefault();  
} 
+0

對不起,沒看到(怪時間),你已經有了「db」實例。我的「實體」服務於相同的目的。 – DorkyMork

+0

這對我有效 - 感謝您的幫助和歡迎堆棧溢出! – tintyethan

+0

謝謝!祝你好運! – DorkyMork

0

,你可以這樣做:

int cid = 1; 
int bid = 2; 

var sendto = from businesscontact in db.businesscontacts 
      join contacts in db.contacts 
      on businesscontact.contactsid equals contacts.contactid 
      join contactscontactcodes in db.contactscontactcodes 
      on contacts.contactsid equals contactscontactcodes.contactsid 
      where contactscontactcodes.contactcodesid = cid 
      && businesscontacts.businessid = bid 
      select contact.email;