Sorting by a DateTime Field Ignoring The Year

3 posts, 1 answers
  1. Alexandre
    Alexandre avatar
    10 posts
    Member since:
    May 2009

    Posted 14 Nov 2012 Link to this post

    Hi,

    How can i use a orderby clause using a DateTime field ignoring the year?

    Is that possible?

    Thanks
  2. Answer
    Dimitar Tachev
    Admin
    Dimitar Tachev avatar
    67 posts

    Posted 16 Nov 2012 Link to this post

    Hi Alexandre,

    In order to use the OrderBy clause on a DateTime field ignoring the year I suggest you to base your query on the following code snippet which is retrieving data from the Orders table of the Northwind database.         

    ...
    using (YourContextName db = new YourContextName())
    {
        var orderByDateTimeWithoutYear = db.Orders
            .Where(x => x.OrderDate.HasValue)
            .OrderBy(x => x.OrderDate.Value.Month)
            .ThenBy(x => x.OrderDate.Value.Day);
          ...
    }
    ...
    If you want to become more familiar with the LINQ capabilities of OpenAccess ORM you can download our SDK and look at our Linq 101 sample.

    I hope this helps.


    Regards,
    Dimitar Tachev
    the Telerik team
    Telerik OpenAccess ORM Meets ASP.NET Web API. Read more.
  3. DevCraft banner
  4. Alexandre
    Alexandre avatar
    10 posts
    Member since:
    May 2009

    Posted 21 Nov 2012 Link to this post

    Thanks Dimitar!

    Works perfectly!
Back to Top