CRM 2011 Linq Retrieve Many to Many N:N Records

Hiya,

As you all know, you can use Query Expressions or FetchXML to retrieve N:N records. Good news, we can also use LINQ if you wish to.

Let's say we have a portal account (for website login) that has N:N to organisation and we want to grab all portal accounts for a given organisation (in my case I have early bound entities):


1
2
3
4
5
6
7
8
var testOrg = serviceContext.AccountSet.where(a => a.Name == "My Org").FirstOrDefault();
 
var portalAccs = (from pa in serviceContext.CreateQuery<new_portalaccount>()
                  join r in serviceContext.CreateQuery<new_portalaccount_account>() on pa.Id equals r.new_portalaccountid
                  join o in serviceContext.CreateQuery<Account>() on r.accountid equals o.AccountId
                  where o.AccountId == testOrg.AccountId
                  where pa.statecode == new_portalaccountState.Active
                  select pa).ToList();


Easy? :)

HTH,
Andreas




Comments

Popular posts from this blog

SharePoint 2013 anonymous access add attachments to list item

CRM Plugin - Parent and Child Pipeline