Skip to main content

Notifications

Announcements

No record found.

Microsoft Dynamics CRM (Archived)

QueryExpression : How do I add more than one order?

(0) ShareShare
ReportReport
Posted on by Microsoft Employee

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

  • Suggested answer
    Alagunellaikumar Profile Picture
    Alagunellaikumar 6,210 on at
    RE: QueryExpression : How do I add more than one order?

    Hi

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

  • Suggested answer
    Yadnyesh Kuvalekar Profile Picture
    Yadnyesh Kuvalekar 4,102 on at
    RE: QueryExpression : How do I add more than one order?

    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
    Felippe Profile Picture
    Felippe 796 on at
    RE: QueryExpression : How do I add more than one order?

    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.

  • Community Member Profile Picture
    Community Member Microsoft Employee on at
    RE: QueryExpression : How do I add more than one order?

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

  • Suggested answer
    Mahadeo Matre Profile Picture
    Mahadeo Matre 17,021 on at
    RE: QueryExpression : How do I add more than one order?

    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
    Community Member Microsoft Employee on at
    RE: QueryExpression : How do I add more than one order?

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

  • Felippe Profile Picture
    Felippe 796 on at
    RE: QueryExpression : How do I add more than one order?

    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"

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

December Spotlight Star - Muhammad Affan

Congratulations to a top community star!

Top 10 leaders for November!

Congratulations to our November super stars!

Tips for Writing Effective Suggested Answers

Best practices for providing successful forum answers ✍️

Leaderboard

#1
André Arnaud de Calavon Profile Picture

André Arnaud de Cal... 291,280 Super User 2024 Season 2

#2
Martin Dráb Profile Picture

Martin Dráb 230,235 Most Valuable Professional

#3
nmaenpaa Profile Picture

nmaenpaa 101,156

Leaderboard

Featured topics

Product updates

Dynamics 365 release plans