{"id":3254,"date":"2018-08-28T07:53:53","date_gmt":"2018-08-28T07:53:53","guid":{"rendered":"http:\/\/himbap.com\/blog\/?p=3254"},"modified":"2018-08-28T07:53:53","modified_gmt":"2018-08-28T07:53:53","slug":"query-intersect-entity-and-its-attribute-sample-code","status":"publish","type":"post","link":"https:\/\/himbap.com\/blog\/?p=3254","title":{"rendered":"Query intersect entity and it&#8217;s attribute- Sample Code"},"content":{"rendered":"<p><strong>Introduction<\/strong><br \/>\nWhen we setup system N:N relationship between two entities an intersect entity is created internally, which is not accessible directly. Let\u2019s say we have N:N relationship between case and workorder entity.<br \/>\n<a href=\"https:\/\/himbap.com\/blog\/wp-content\/uploads\/2018\/08\/intersect1.png\"><img decoding=\"async\" loading=\"lazy\" class=\"alignnone size-medium wp-image-3255 aligncenter\" src=\"https:\/\/himbap.com\/blog\/wp-content\/uploads\/2018\/08\/intersect1-300x184.png\" alt=\"intersect1\" width=\"300\" height=\"184\" srcset=\"https:\/\/himbap.com\/blog\/wp-content\/uploads\/2018\/08\/intersect1-300x184.png 300w, https:\/\/himbap.com\/blog\/wp-content\/uploads\/2018\/08\/intersect1-624x383.png 624w, https:\/\/himbap.com\/blog\/wp-content\/uploads\/2018\/08\/intersect1.png 744w\" sizes=\"(max-width: 300px) 100vw, 300px\" \/><\/a><br \/>\nAnd if you will try to see attributes of this entity, you will find following attributes<br \/>\n<a href=\"https:\/\/himbap.com\/blog\/wp-content\/uploads\/2018\/08\/intersect2.png\"><img decoding=\"async\" loading=\"lazy\" class=\"alignnone size-medium wp-image-3257 aligncenter\" src=\"https:\/\/himbap.com\/blog\/wp-content\/uploads\/2018\/08\/intersect2-300x81.png\" alt=\"intersect2\" width=\"300\" height=\"81\" srcset=\"https:\/\/himbap.com\/blog\/wp-content\/uploads\/2018\/08\/intersect2-300x81.png 300w, https:\/\/himbap.com\/blog\/wp-content\/uploads\/2018\/08\/intersect2-624x168.png 624w, https:\/\/himbap.com\/blog\/wp-content\/uploads\/2018\/08\/intersect2.png 776w\" sizes=\"(max-width: 300px) 100vw, 300px\" \/><\/a><br \/>\nSo in case we want to query this entity, we need to query it based on the the related entities. If you are looking for the sample code to query intersect entity and get it&#8217;s attribute this post will help you.<\/p>\n<p><strong>Requirement<\/strong><br \/>\nQuery above intersect entity and check if there is any work order associated to case, if yes we want to get workorderid.<\/p>\n<p><strong>Solution<\/strong><br \/>\nAs said we can&#8217;t query intersect entity directly but we can query it using case or work order, in our case we will be query it with case entity. We are going to use <a href=\"https:\/\/docs.microsoft.com\/en-us\/dynamics365\/customer-engagement\/developer\/org-service\/build-queries-with-queryexpression\">QueryExpression <\/a>class here and will be using it&#8217;s <a href=\"https:\/\/docs.microsoft.com\/en-us\/dotnet\/api\/microsoft.xrm.sdk.query.queryexpression.addlink?view=dynamics-general-ce-9\">AddLink <\/a>method to get data from intersect entity. We can use following code<\/p>\n<pre class=\"brush: csharp; title: ; notranslate\" title=\"\">\r\n\/\/Create Query Expression.\r\n                        QueryExpression query = new QueryExpression()\r\n                        {\r\n                            EntityName = &quot;incident&quot;,\r\n                            ColumnSet = new ColumnSet(&quot;title&quot;),\r\n                            LinkEntities =\r\n                        {\r\n                            new LinkEntity\r\n                            {\r\n                                LinkToEntityName = &quot;him_incident_msdyn_workorder&quot;, \/\/name of the intersect entity\r\n                                LinkFromAttributeName=&quot;incidentid&quot;, \r\n                                LinkToAttributeName = &quot;incidentid&quot;,\r\n                                Columns=new ColumnSet(new string[]{&quot;msdyn_workorderid&quot; }), \/\/workorder id in interset entity\r\n                                EntityAlias=&quot;intersect&quot;, \/\/add alias name to refer attribute easily \r\n                                LinkCriteria = new FilterExpression\r\n                                {\r\n                                    FilterOperator = LogicalOperator.And,\r\n                                    Conditions =\r\n                                    {\r\n                                        new ConditionExpression\r\n                                        {\r\n                                            AttributeName = &quot;incidentid&quot;,\r\n                                            Operator = ConditionOperator.Equal,\r\n                                            Values = { caseid }\r\n                                        }\r\n                                    }\r\n                                }\r\n                                \r\n                            }\r\n                        }\r\n                        };\r\n                       \/\/ Obtain results from the query expression.\r\n                        EntityCollection results = service.RetrieveMultiple(query);\r\n<\/pre>\n<p>Now if get workorderid we need to use <a href=\"https:\/\/msdn.microsoft.com\/en-us\/library\/microsoft.xrm.sdk.aliasedvalue.aspx\">AliasedValue <\/a>class here, for example to get first workorderid, we need to use following code<\/p>\n<pre class=\"brush: csharp; title: ; notranslate\" title=\"\">\r\nGuid workorderid =new Guid(result.Entities[0].GetAttributeValue&lt;AliasedValue&gt;(&quot;intersect.msdyn_workorderid&quot;).Value.ToString());\r\n<\/pre>\n","protected":false},"excerpt":{"rendered":"<p>Introduction When we setup system N:N relationship between two entities an intersect entity is created internally, which is not accessible directly. Let\u2019s say we have N:N relationship between case and workorder entity. And if you will try to see attributes of this entity, you will find following attributes So in case we want to query this entity, we need to&#8230; <a href=\"https:\/\/himbap.com\/blog\/?p=3254\">Read more &raquo;<\/a><\/p>\n","protected":false},"author":1,"featured_media":0,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":[],"categories":[402,522,5,6,8,275,20],"tags":[611,609,610,608,613,612],"_links":{"self":[{"href":"https:\/\/himbap.com\/blog\/index.php?rest_route=\/wp\/v2\/posts\/3254"}],"collection":[{"href":"https:\/\/himbap.com\/blog\/index.php?rest_route=\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/himbap.com\/blog\/index.php?rest_route=\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/himbap.com\/blog\/index.php?rest_route=\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/himbap.com\/blog\/index.php?rest_route=%2Fwp%2Fv2%2Fcomments&post=3254"}],"version-history":[{"count":3,"href":"https:\/\/himbap.com\/blog\/index.php?rest_route=\/wp\/v2\/posts\/3254\/revisions"}],"predecessor-version":[{"id":3259,"href":"https:\/\/himbap.com\/blog\/index.php?rest_route=\/wp\/v2\/posts\/3254\/revisions\/3259"}],"wp:attachment":[{"href":"https:\/\/himbap.com\/blog\/index.php?rest_route=%2Fwp%2Fv2%2Fmedia&parent=3254"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/himbap.com\/blog\/index.php?rest_route=%2Fwp%2Fv2%2Fcategories&post=3254"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/himbap.com\/blog\/index.php?rest_route=%2Fwp%2Fv2%2Ftags&post=3254"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}