Tuesday 2 July 2013

Retrieve N:N records in MS CRM 2011

Retrieving N:N records in MS CRM 2011 using LINQ requires less code and is straightforward.

I am assuming that "Account" is in N:N relationship with "Custom Entity".

Below is the screenshot for relationship





















Here is the Screenshot of account having custom entity records





























Query:

LINQ query to get all the N:N custom entity records for the above Account.

 string accountId = "CC572EC9-01E3-E211-AC62-984BE173A384";
  var customEntities = (from NtoN in makeUseOfContext.new_customentity_accountSet
                        where NtoN.accountid.Value == new Guid(accountId) && 
                        NtoN.new_customentityid != null
                         select new { 
                                    customentityid = NtoN.new_customentityid.Value 
                                   }).ToList();
        
Debug Mode:

















Similarly we can get the Accounts associated to the custom  entity.
Hope it will help someone somewhere :)

Thanks,
Yusuf

1 comment: