web
You’re offline. This is a read only version of the page.
close
Skip to main content

Notifications

Announcements

No record found.

Community site session details

Community site session details

Session Id :
Microsoft Dynamics CRM (Archived)

Select Maximum number from a numeric field in an entity.

(0) ShareShare
ReportReport
Posted on by 20

Hi guys, what is the best way to retrieve the maximum value from a numeric field in an entity?

Something like this in SQL Server: Select MAX(NumbericFieldName) From TableName. 

Maybe: 

var maxZ = list.Max( obj => obj.Z ); is this possible?

Thanks,

john

*This post is locked for comments

I have the same question (0)
  • Verified answer
    ashlega Profile Picture
    34,477 on at

    Hi John,

      it's probably best to use FetchXml aggregation:

    https://msdn.microsoft.com/en-us/library/gg309565.aspx#max

      That way, you won't have to get all those records on the client.

  • John Frometa Profile Picture
    20 on at

    Well, your answer (using fetchxml) should be the appropriate way, however since I am using the ADX Studio Portal source source code I was not able to execute a fetch as in the example shown. I had to use a bad practice like this:

    var document = XrmContext.CreateQuery("entityname").OrderByDescending(d => d.GetAttributeValue<Int32?>("attributename"));
    var documentno = document.Select(d => d.GetAttributeValue<Int32?>("attributename")).FirstOrDefault();

    Thank you,

    john

  • ashlega Profile Picture
    34,477 on at

    You might actually try

    var document = XrmContext.CreateQuery("entityname").OrderByDescending(d => d.GetAttributeValue<Int32?>("attributename")).Take(1);

    var documentno = document.Select(d => d.GetAttributeValue<Int32?>("attributename")).FirstOrDefault();

    I'm not sure, but it may be more efficient (assuming that'll be translated into a proper fetch.. which it should)

Under review

Thank you for your reply! To ensure a great experience for everyone, your content is awaiting approval by our Community Managers. Please check back later.

Helpful resources

Quick Links

Responsible AI policies

As AI tools become more common, we’re introducing a Responsible AI Use…

Neeraj Kumar – Community Spotlight

We are honored to recognize Neeraj Kumar as our Community Spotlight honoree for…

Leaderboard > 🔒一 Microsoft Dynamics CRM (Archived)

#1
SA-08121319-0 Profile Picture

SA-08121319-0 4

#1
Calum MacFarlane Profile Picture

Calum MacFarlane 4

#3
Alex Fun Wei Jie Profile Picture

Alex Fun Wei Jie 2

Last 30 days Overall leaderboard

Featured topics

Product updates

Dynamics 365 release plans