locked
Ordering FetchXML results by linked entities' attributes RRS feed

  • Question

  • Hi Devs,

    Is there any way, I can filter FetchXML results by linked entities' attributes?

    See the sample FetchXML below. I want to order the fetched results by mysol_entity2.mysol_orderattribute first then mysol_entity1.orderattribute. I have 4 linked entities in an actual FetchXML and the result should ideally be ordered by one attribute from each linked entity and a main entity.

    <fetch version="1.0" output-format="xml-platform" mapping="logical" distinct="false" >
        <entity name="mysol_entity1" >
            <attribute name="mysol_orderattribute" />
            <order attribute="mysol_orderattribute" descending="false" />
            <link-entity name="mysol_entity2" from="mysol_entity2id" to="mysol_entity2" visible="false" link-type="outer" alias="entity2" >
                <attribute name="mysol_orderattribute" />
            </link-entity>
        </entity>
    </fetch>

    Best regards,


    blog: http://technologynotesforyou.wordpress.com | skype: ali.net.pk

    Friday, March 11, 2016 3:08 PM

Answers

All replies

  • Hello,

    I'm not sure that you can do it in FetchXml. Anyway you can sort it after result on your side using Linq.


    Dynamics CRM MVP
    Read My blog
    Subscribe for one of my courses

    Friday, March 11, 2016 3:44 PM
    Moderator
  • Hello,

    I'm not sure that you can do it in FetchXml. Anyway you can sort it after result on your side using Linq.


    Dynamics CRM MVP
    Read My blog
    Subscribe for one of my courses

    Thanks Andrii,

    Yeah, I did it using LINQ, but was looking for a FetchXML way for doing this.


    blog: <a href="http://technologynotesforyou.wordpress.com">http://technologynotesforyou.wordpress.com</a> | skype: ali.net.pk

    Friday, March 11, 2016 5:17 PM
  • Just to complete the thread, here is the sample code to sort the FetchXML results on linked entities' attributes using LINQ:

    foreach (var item in result.Entities
    	.ThenBy(e => ((AliasedValue)e.Attributes["entity2.mysol_orderattribute"]).Value.ToString())
    	.ThenBy(e => e.Attributes["mysol_orderattribute"].ToString()))
    {
    	string entity2OrderValue = ((AliasedValue)item.Attributes["entity2.mysol_orderattribute"]).Value.ToString();
    	string entity1OrderValue = item.Attributes["mysol_orderattribute"].ToString();
    	string entityName = item.Attributes["mysol_name"].ToString();
    
    	Console.WriteLine("entity2OrderValue: {0}, entity1OrderValue: {2}, entityName: {3}", entity2OrderValue, entity1OrderValue, entityName);
    }
    



    blog: <a href="http://technologynotesforyou.wordpress.com">http://technologynotesforyou.wordpress.com</a> | skype: ali.net.pk

    Saturday, March 12, 2016 2:57 PM