Here I'm taking Contact and Account has N:N Relationship.
---------------------------------------------------------------------
Now I wanted to Retrieve all Contacts which are Associated with Accounts.
*********************************************************************************
QueryExpression query = new QueryExpression("new_conatct");
query.ColumnSet = new ColumnSet(true);
LinkEntity contactToAccount = new LinkEntity("new_contact", "new_account_new_conatct", "new_conatctid", "new_contactid", JoinOperator.Inner);
contactToAccount.EntityAlias = "ContactAccount";
contactToAccount .Columns.AddColumn("new_accountid");
query.LinkEntities.Add(contactToAccount);
EntityCollection collAcc = _service.RetrieveMultiple(query);
foreach( Entity record in collAcc.Entities)
{
// your code goes to here
}
---------------------------------------------------------------------
Now I wanted to Retrieve all Contacts which are Associated with Accounts.
*********************************************************************************
QueryExpression query = new QueryExpression("new_conatct");
query.ColumnSet = new ColumnSet(true);
LinkEntity contactToAccount = new LinkEntity("new_contact", "new_account_new_conatct", "new_conatctid", "new_contactid", JoinOperator.Inner);
contactToAccount.EntityAlias = "ContactAccount";
contactToAccount .Columns.AddColumn("new_accountid");
query.LinkEntities.Add(contactToAccount);
EntityCollection collAcc = _service.RetrieveMultiple(query);
foreach( Entity record in collAcc.Entities)
{
// your code goes to here
}