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)

QueryExpression : How do I add more than one order?

(0) ShareShare
ReportReport
Posted on by

Is there a way to specify a multi-sort either as the query is being built or on the result set outputted by the QueryExpression?

*This post is locked for comments

I have the same question (0)
  • Felippe Profile Picture
    796 on at

    Have u already tried mutiple .AddOrder on your Query expression?

    Or use LINQ for your result and try multiple sort like this link suggest:

    stackoverflow.com/.../multiple-order-by-in-linq

    If this solves, please Like and mark as "Verified Answer"

  • Community Member Profile Picture
    on at

    Already tried multiple AddOrder methods and that did not work. To use LINQ I'd have to load CRM classes (using Xrm.cs) right?

  • Suggested answer
    Mahadeo Matre Profile Picture
    17,021 on at

    you add multiple sort orders when using queryexpression

    Microsoft.Xrm.Sdk.Query.QueryExpression query = new Microsoft.Xrm.Sdk.Query.QueryExpression();

               query.Orders.Add(new Microsoft.Xrm.Sdk.Query.OrderExpression("firstname", Microsoft.Xrm.Sdk.Query.OrderType.Ascending));

               query.Orders.Add(new Microsoft.Xrm.Sdk.Query.OrderExpression("lastname", Microsoft.Xrm.Sdk.Query.OrderType.Ascending));

               query.Orders.Add(new Microsoft.Xrm.Sdk.Query.OrderExpression("birthdate", Microsoft.Xrm.Sdk.Query.OrderType.Ascending));

    also check

    msdn.microsoft.com/.../microsoft.xrm.sdk.query.queryexpression.orders.aspx

    msdn.microsoft.com/.../microsoft.xrm.sdk.query.queryexpression.addorder.aspx

    www.magnetismsolutions.com/.../dynamics_crm_2011_querying_data_with_queryexpression

  • Community Member Profile Picture
    on at

    Thank you for your answer. Is it possible to add an order attribute from a related entity?

  • Suggested answer
    Felippe Profile Picture
    796 on at

    For LINQ with early bound, you will need the code generation utility:

    msdn.microsoft.com/.../gg327844.aspx

    For LINQ with late bound (I haven' tried this method yet):

    msdn.microsoft.com/.../gg334415.aspx

    or

    msdn.microsoft.com/.../Hh210215.aspx

    If this became too tricky, maybe you can break your query in two:

    Apply the "main order" on the first query, and over the result, apply the second order filter.

  • Suggested answer
    Yadnyesh Kuvalekar Profile Picture
    4,102 on at

    This is what you are expecting:

    QueryExpression query = new QueryExpression("opportunity") {
                    ColumnSet=new ColumnSet("topic")
                };
                OrderExpression order1 = new OrderExpression("topic", OrderType.Ascending);
                OrderExpression order2 = new OrderExpression("createdon", OrderType.Descending);
                query.Orders.Add(order1);
                query.Orders.Add(order2);


    Also, you cannot add order attributes from related entity.

  • Suggested answer
    Alagunellaikumar Profile Picture
    6,212 on at

    Hi

    Related entity adding order by is not possible why don't use fetch xml in plugin?

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