{"id":35,"date":"2020-11-02T17:39:25","date_gmt":"2020-11-02T09:39:25","guid":{"rendered":"https:\/\/just-a-web.com\/blog\/?p=35"},"modified":"2020-11-02T17:42:30","modified_gmt":"2020-11-02T09:42:30","slug":"retrieve-n-to-n-relationship-via-csharp","status":"publish","type":"post","link":"https:\/\/just-a-web.com\/blog\/2020\/11\/02\/retrieve-n-to-n-relationship-via-csharp\/","title":{"rendered":"Retrieve N to N Relationship via C#"},"content":{"rendered":"\n<p>N to N relationship is not so common as far as I know due to the complexity of the design, however there are some use cases where it is needed, example: N contact can have N hobby.<\/p>\n\n\n\n<p>Below is the code of how to retrieve hobby by filtering contact GUID:<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code lang=\"csharp\" class=\"language-csharp\">var query = new QueryExpression(Hobby.EntityName)\r\n{\r\n\tColumnSet = new ColumnSet(Hobby.PrimaryKey),\r\n\tLinkEntities =\r\n\t{\r\n\t\tnew LinkEntity(Hobby.EntityName, app_hobby_contact.EntityName, Hobby.PrimaryKey, app_hobby_contact.app_hobbyid, JoinOperator.Inner)\r\n\t\t{\r\n\t\t\tLinkEntities =\r\n\t\t\t{\r\n\t\t\t\tnew LinkEntity(app_hobby_contact.EntityName, Contact.EntityName, app_hobby_contact.contactid, Contact.PrimaryKey, JoinOperator.Inner)\r\n\t\t\t\t{\r\n\t\t\t\t\tLinkCriteria = new FilterExpression(LogicalOperator.And)\r\n\t\t\t\t\t{\r\n\t\t\t\t\t\tConditions =\r\n\t\t\t\t\t\t{\r\n\t\t\t\t\t\t\tnew ConditionExpression(Contact.PrimaryKey, ConditionOperator.Equal, contactId)\r\n\t\t\t\t\t\t},\r\n\t\t\t\t\t}\r\n\t\t\t\t},\r\n\t\t\t},\r\n\t\t},\r\n\t},\r\n};<\/code><\/pre>\n\n\n\n<p>To further explain the above code, we use Hobby as our main entity and link to the relationship (app_hobby_contact), and after that we link again from our relationship to Contact and do a filter criteria on selected contact GUID.<\/p>\n\n\n\n<p>It looks complicated but it is useful when you need retrieve some of the attributes here.<\/p>\n\n\n\n<p>If you just need the hobby GUID, we may use below code:<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code lang=\"csharp\" class=\"language-csharp\">var query = new QueryExpression(app_hobby_contact.EntityName)\n{\n\tColumnSet = new ColumnSet(app_hobby_contact.app_hobbyid),\n\tCriteria = new FilterExpression(LogicalOperator.And)\n\t{\n\t\tConditions =\n\t\t{\n\t\t\tnew ConditionExpression(app_hobby_contact.contactid, ConditionOperator.Equal, contactId)\n\t\t}\n\t}\n};\n<\/code><\/pre>\n\n\n\n<p>This code is just retrieve from the relationship and do a filter on the contact GUID, and we are able to get the hobby GUID(s) that own by the contact easily without doing much linked entities and the code is easily to understand.<\/p>\n","protected":false},"excerpt":{"rendered":"<p>N to N relationship is not so common as far as I know due to the complexity of the design, however there are some use cases where it is needed, example: N contact can have N hobby. Below is the code of how to retrieve hobby by filtering contact GUID: To further explain the above&hellip;&nbsp;<a href=\"https:\/\/just-a-web.com\/blog\/2020\/11\/02\/retrieve-n-to-n-relationship-via-csharp\/\" rel=\"bookmark\">Read More &raquo;<span class=\"screen-reader-text\">Retrieve N to N Relationship via C#<\/span><\/a><\/p>\n","protected":false},"author":1,"featured_media":0,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"neve_meta_sidebar":"","neve_meta_container":"","neve_meta_enable_content_width":"","neve_meta_content_width":0,"neve_meta_title_alignment":"","neve_meta_author_avatar":"","neve_post_elements_order":"","neve_meta_disable_header":"","neve_meta_disable_footer":"","neve_meta_disable_title":"","footnotes":""},"categories":[14,1],"tags":[15,17,16],"class_list":["post-35","post","type-post","status-publish","format-standard","hentry","category-c","category-dynamics","tag-nton","tag-retrieve","tag-sdk"],"yoast_head":"<!-- This site is optimized with the Yoast SEO plugin v25.3.1 - https:\/\/yoast.com\/wordpress\/plugins\/seo\/ -->\n<title>Retrieve N to N Relationship via C# - Dynamics Blog<\/title>\n<meta name=\"description\" content=\"Sample code by using SDK to retrieve N to N Relationship in Dynamics CRM \/ 365\" \/>\n<meta name=\"robots\" content=\"index, follow, max-snippet:-1, max-image-preview:large, max-video-preview:-1\" \/>\n<link rel=\"canonical\" href=\"https:\/\/just-a-web.com\/blog\/2020\/11\/02\/retrieve-n-to-n-relationship-via-csharp\/\" \/>\n<meta property=\"og:locale\" content=\"en_US\" \/>\n<meta property=\"og:type\" content=\"article\" \/>\n<meta property=\"og:title\" content=\"Retrieve N to N Relationship via C# - Dynamics Blog\" \/>\n<meta property=\"og:description\" content=\"Sample code by using SDK to retrieve N to N Relationship in Dynamics CRM \/ 365\" \/>\n<meta property=\"og:url\" content=\"https:\/\/just-a-web.com\/blog\/2020\/11\/02\/retrieve-n-to-n-relationship-via-csharp\/\" \/>\n<meta property=\"og:site_name\" content=\"Dynamics Blog\" \/>\n<meta property=\"article:published_time\" content=\"2020-11-02T09:39:25+00:00\" \/>\n<meta property=\"article:modified_time\" content=\"2020-11-02T09:42:30+00:00\" \/>\n<meta name=\"author\" content=\"hk.ang\" \/>\n<meta name=\"twitter:card\" content=\"summary_large_image\" \/>\n<meta name=\"twitter:label1\" content=\"Written by\" \/>\n\t<meta name=\"twitter:data1\" content=\"hk.ang\" \/>\n\t<meta name=\"twitter:label2\" content=\"Est. reading time\" \/>\n\t<meta name=\"twitter:data2\" content=\"1 minute\" \/>\n<script type=\"application\/ld+json\" class=\"yoast-schema-graph\">{\"@context\":\"https:\/\/schema.org\",\"@graph\":[{\"@type\":\"Article\",\"@id\":\"https:\/\/just-a-web.com\/blog\/2020\/11\/02\/retrieve-n-to-n-relationship-via-csharp\/#article\",\"isPartOf\":{\"@id\":\"https:\/\/just-a-web.com\/blog\/2020\/11\/02\/retrieve-n-to-n-relationship-via-csharp\/\"},\"author\":{\"name\":\"hk.ang\",\"@id\":\"https:\/\/just-a-web.com\/blog\/#\/schema\/person\/7c5fde715b55adf97070f3506709b0af\"},\"headline\":\"Retrieve N to N Relationship via C#\",\"datePublished\":\"2020-11-02T09:39:25+00:00\",\"dateModified\":\"2020-11-02T09:42:30+00:00\",\"mainEntityOfPage\":{\"@id\":\"https:\/\/just-a-web.com\/blog\/2020\/11\/02\/retrieve-n-to-n-relationship-via-csharp\/\"},\"wordCount\":170,\"commentCount\":0,\"publisher\":{\"@id\":\"https:\/\/just-a-web.com\/blog\/#\/schema\/person\/7c5fde715b55adf97070f3506709b0af\"},\"keywords\":[\"NtoN\",\"Retrieve\",\"SDK\"],\"articleSection\":[\"C#\",\"Dynamics\"],\"inLanguage\":\"en-US\",\"potentialAction\":[{\"@type\":\"CommentAction\",\"name\":\"Comment\",\"target\":[\"https:\/\/just-a-web.com\/blog\/2020\/11\/02\/retrieve-n-to-n-relationship-via-csharp\/#respond\"]}]},{\"@type\":\"WebPage\",\"@id\":\"https:\/\/just-a-web.com\/blog\/2020\/11\/02\/retrieve-n-to-n-relationship-via-csharp\/\",\"url\":\"https:\/\/just-a-web.com\/blog\/2020\/11\/02\/retrieve-n-to-n-relationship-via-csharp\/\",\"name\":\"Retrieve N to N Relationship via C# - Dynamics Blog\",\"isPartOf\":{\"@id\":\"https:\/\/just-a-web.com\/blog\/#website\"},\"datePublished\":\"2020-11-02T09:39:25+00:00\",\"dateModified\":\"2020-11-02T09:42:30+00:00\",\"description\":\"Sample code by using SDK to retrieve N to N Relationship in Dynamics CRM \/ 365\",\"breadcrumb\":{\"@id\":\"https:\/\/just-a-web.com\/blog\/2020\/11\/02\/retrieve-n-to-n-relationship-via-csharp\/#breadcrumb\"},\"inLanguage\":\"en-US\",\"potentialAction\":[{\"@type\":\"ReadAction\",\"target\":[\"https:\/\/just-a-web.com\/blog\/2020\/11\/02\/retrieve-n-to-n-relationship-via-csharp\/\"]}]},{\"@type\":\"BreadcrumbList\",\"@id\":\"https:\/\/just-a-web.com\/blog\/2020\/11\/02\/retrieve-n-to-n-relationship-via-csharp\/#breadcrumb\",\"itemListElement\":[{\"@type\":\"ListItem\",\"position\":1,\"name\":\"Home\",\"item\":\"https:\/\/just-a-web.com\/blog\/\"},{\"@type\":\"ListItem\",\"position\":2,\"name\":\"Retrieve N to N Relationship via C#\"}]},{\"@type\":\"WebSite\",\"@id\":\"https:\/\/just-a-web.com\/blog\/#website\",\"url\":\"https:\/\/just-a-web.com\/blog\/\",\"name\":\"Dynamics Blog\",\"description\":\"A blog that related to coding stuff\",\"publisher\":{\"@id\":\"https:\/\/just-a-web.com\/blog\/#\/schema\/person\/7c5fde715b55adf97070f3506709b0af\"},\"potentialAction\":[{\"@type\":\"SearchAction\",\"target\":{\"@type\":\"EntryPoint\",\"urlTemplate\":\"https:\/\/just-a-web.com\/blog\/?s={search_term_string}\"},\"query-input\":{\"@type\":\"PropertyValueSpecification\",\"valueRequired\":true,\"valueName\":\"search_term_string\"}}],\"inLanguage\":\"en-US\"},{\"@type\":[\"Person\",\"Organization\"],\"@id\":\"https:\/\/just-a-web.com\/blog\/#\/schema\/person\/7c5fde715b55adf97070f3506709b0af\",\"name\":\"hk.ang\",\"logo\":{\"@id\":\"https:\/\/just-a-web.com\/blog\/#\/schema\/person\/image\/\"},\"sameAs\":[\"https:\/\/just-a-web.com\/blog\"]}]}<\/script>\n<!-- \/ Yoast SEO plugin. -->","yoast_head_json":{"title":"Retrieve N to N Relationship via C# - Dynamics Blog","description":"Sample code by using SDK to retrieve N to N Relationship in Dynamics CRM \/ 365","robots":{"index":"index","follow":"follow","max-snippet":"max-snippet:-1","max-image-preview":"max-image-preview:large","max-video-preview":"max-video-preview:-1"},"canonical":"https:\/\/just-a-web.com\/blog\/2020\/11\/02\/retrieve-n-to-n-relationship-via-csharp\/","og_locale":"en_US","og_type":"article","og_title":"Retrieve N to N Relationship via C# - Dynamics Blog","og_description":"Sample code by using SDK to retrieve N to N Relationship in Dynamics CRM \/ 365","og_url":"https:\/\/just-a-web.com\/blog\/2020\/11\/02\/retrieve-n-to-n-relationship-via-csharp\/","og_site_name":"Dynamics Blog","article_published_time":"2020-11-02T09:39:25+00:00","article_modified_time":"2020-11-02T09:42:30+00:00","author":"hk.ang","twitter_card":"summary_large_image","twitter_misc":{"Written by":"hk.ang","Est. reading time":"1 minute"},"schema":{"@context":"https:\/\/schema.org","@graph":[{"@type":"Article","@id":"https:\/\/just-a-web.com\/blog\/2020\/11\/02\/retrieve-n-to-n-relationship-via-csharp\/#article","isPartOf":{"@id":"https:\/\/just-a-web.com\/blog\/2020\/11\/02\/retrieve-n-to-n-relationship-via-csharp\/"},"author":{"name":"hk.ang","@id":"https:\/\/just-a-web.com\/blog\/#\/schema\/person\/7c5fde715b55adf97070f3506709b0af"},"headline":"Retrieve N to N Relationship via C#","datePublished":"2020-11-02T09:39:25+00:00","dateModified":"2020-11-02T09:42:30+00:00","mainEntityOfPage":{"@id":"https:\/\/just-a-web.com\/blog\/2020\/11\/02\/retrieve-n-to-n-relationship-via-csharp\/"},"wordCount":170,"commentCount":0,"publisher":{"@id":"https:\/\/just-a-web.com\/blog\/#\/schema\/person\/7c5fde715b55adf97070f3506709b0af"},"keywords":["NtoN","Retrieve","SDK"],"articleSection":["C#","Dynamics"],"inLanguage":"en-US","potentialAction":[{"@type":"CommentAction","name":"Comment","target":["https:\/\/just-a-web.com\/blog\/2020\/11\/02\/retrieve-n-to-n-relationship-via-csharp\/#respond"]}]},{"@type":"WebPage","@id":"https:\/\/just-a-web.com\/blog\/2020\/11\/02\/retrieve-n-to-n-relationship-via-csharp\/","url":"https:\/\/just-a-web.com\/blog\/2020\/11\/02\/retrieve-n-to-n-relationship-via-csharp\/","name":"Retrieve N to N Relationship via C# - Dynamics Blog","isPartOf":{"@id":"https:\/\/just-a-web.com\/blog\/#website"},"datePublished":"2020-11-02T09:39:25+00:00","dateModified":"2020-11-02T09:42:30+00:00","description":"Sample code by using SDK to retrieve N to N Relationship in Dynamics CRM \/ 365","breadcrumb":{"@id":"https:\/\/just-a-web.com\/blog\/2020\/11\/02\/retrieve-n-to-n-relationship-via-csharp\/#breadcrumb"},"inLanguage":"en-US","potentialAction":[{"@type":"ReadAction","target":["https:\/\/just-a-web.com\/blog\/2020\/11\/02\/retrieve-n-to-n-relationship-via-csharp\/"]}]},{"@type":"BreadcrumbList","@id":"https:\/\/just-a-web.com\/blog\/2020\/11\/02\/retrieve-n-to-n-relationship-via-csharp\/#breadcrumb","itemListElement":[{"@type":"ListItem","position":1,"name":"Home","item":"https:\/\/just-a-web.com\/blog\/"},{"@type":"ListItem","position":2,"name":"Retrieve N to N Relationship via C#"}]},{"@type":"WebSite","@id":"https:\/\/just-a-web.com\/blog\/#website","url":"https:\/\/just-a-web.com\/blog\/","name":"Dynamics Blog","description":"A blog that related to coding stuff","publisher":{"@id":"https:\/\/just-a-web.com\/blog\/#\/schema\/person\/7c5fde715b55adf97070f3506709b0af"},"potentialAction":[{"@type":"SearchAction","target":{"@type":"EntryPoint","urlTemplate":"https:\/\/just-a-web.com\/blog\/?s={search_term_string}"},"query-input":{"@type":"PropertyValueSpecification","valueRequired":true,"valueName":"search_term_string"}}],"inLanguage":"en-US"},{"@type":["Person","Organization"],"@id":"https:\/\/just-a-web.com\/blog\/#\/schema\/person\/7c5fde715b55adf97070f3506709b0af","name":"hk.ang","logo":{"@id":"https:\/\/just-a-web.com\/blog\/#\/schema\/person\/image\/"},"sameAs":["https:\/\/just-a-web.com\/blog"]}]}},"_links":{"self":[{"href":"https:\/\/just-a-web.com\/blog\/wp-json\/wp\/v2\/posts\/35","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/just-a-web.com\/blog\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/just-a-web.com\/blog\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/just-a-web.com\/blog\/wp-json\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/just-a-web.com\/blog\/wp-json\/wp\/v2\/comments?post=35"}],"version-history":[{"count":3,"href":"https:\/\/just-a-web.com\/blog\/wp-json\/wp\/v2\/posts\/35\/revisions"}],"predecessor-version":[{"id":38,"href":"https:\/\/just-a-web.com\/blog\/wp-json\/wp\/v2\/posts\/35\/revisions\/38"}],"wp:attachment":[{"href":"https:\/\/just-a-web.com\/blog\/wp-json\/wp\/v2\/media?parent=35"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/just-a-web.com\/blog\/wp-json\/wp\/v2\/categories?post=35"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/just-a-web.com\/blog\/wp-json\/wp\/v2\/tags?post=35"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}