Copied to clipboard

Flag this post as spam?

This post will be reported to the moderators as potential spam to be looked at


  • k 256 posts 654 karma points
    Jul 12, 2017 @ 13:22
    k
    0

    Orderby in umbraco 7

    Hello,

    Is it possible to orderby Name first and then by price ?

    That is : .OrderBy("Name") and if we have two values starting with letter A, then we orderby price.

    thanks,

    kusum

  • Michiel van Felius 2 posts 73 karma points
    Jul 12, 2017 @ 13:51
    Michiel van Felius
    1

    Hi,

    Yes that is possible. To do this use: .OrderBy("Name, Price")

    Or .OrderBy("Name, Price desc")

    More info: https://umbraco.com/blog/umbraco-razor-feature-walkthrough-part-4/

  • David Hutson 48 posts 379 karma points
    Jul 12, 2017 @ 13:57
    David Hutson
    1

    Hi Kusum,

    Yes it's possible to order items however you want if you can write some C# using Linq.

    I recommend you download LinqPad it has a bunch of example files to see how it all works.

    There is also an Umbraco Driver so you can write Linq against Umbraco database.

    Once you have crafted your linq (C#) just add it to your controller action or view (inside Razor).

    Example of fluent Linq

    Var movies = _db.Movies.OrderBy(c => c.Category).ThenBy(n => n.Name)
    

    You can also use Linq to compose and build up your filters.

      var query=(from w in widgets
      where w.Name.Contains("xyz")
      select w);
    
      var result = flag ?
      query.OrderBy(w =>w) :
      query.OrderByDescending(w = w);
    
  • Alex Skrypnyk 6175 posts 24186 karma points MVP 8x admin c-trib
    Jul 12, 2017 @ 15:21
    Alex Skrypnyk
    1

    Hi Kusum

    Better to use Strongly typed objects with Linq methods, there is .ThenBy method available for such cases.

    For example:

    var items = Model.Siblings().OrderBy(x => x.Name).ThenBy(x => x.GetPropertyValue<double>("price"));
    

    Thanks,

    Alex

  • k 256 posts 654 karma points
    Jul 13, 2017 @ 06:16
    k
    100

    Hello,

    Thanks David and Alex for replying.

    It's working.

    Thank you.

Please Sign in or register to post replies

Write your reply to:

Draft