locked
How to select the entity that has a maximum value of an attribute ? RRS feed

  • General discussion

  • Hi,
    I have an Entity Term, and I need to select in my code the entity which has the maximum "start_date".
    Is there to directly do that in my code ?
    I was thinking of retrieving all the terms in my CRM into an EntityCollection then find a way to get the term with the nearest startdate. But I also couldn't find a way to apply a "maximum" function over the returned entitycollection.
    Can you please help me with that  ?

    Thanks,
    M

    • Edited by SB User Wednesday, January 15, 2014 11:51 AM
    Wednesday, January 15, 2014 11:35 AM

All replies

  • Hi,

    You can retrieve it like you do in SQL: Sort by start_date DESC then select TOP 1.

    Using your FetchExpression or QueryExpression

    Example link:https://stackoverflow.com/questions/3236020/is-it-possible-to-limit-the-amount-of-results-returned-in-a-response-when-using

    Hope it helps.

    Wednesday, January 15, 2014 11:54 AM
  • Hi,

    as I am thinking u can get the solution several way. I have trying with Fetch XML.


    string query=@"<fetch version="1.0" output-format="xml-platform" mapping="logical" distinct="true">
      <entity name="term">
        <attribute name="field1" />
        <attribute name="field2" />
        <attribute name="start_date" />
        <order attribute="start_date" descending="false" /> //for sorting
      </entity>

    </fetch>"

    EntityCollection result = _serviceProxy.RetrieveMultiple(new FetchExpression(query));
    
    foreach (var c in result.Entities)
    {

    //do what ever u want

    }

    Thanks

    Sangram

    Please Mark this as answer if u got the solution..:)


    microsoft blog

    Wednesday, January 15, 2014 5:49 PM
  • as fetch mention by sangram use can add count=1 in this fetch to get top 1 record

    Hope this helps. ----------------------------------------------------------------------- Santosh Bhagat If this post answers your question, please click "Mark As Answer" on the post and "Vote as Helpful"

    Friday, January 17, 2014 4:33 AM