Using LINQ to Sort by Day of Week And Time

So a couple weeks ago I saw a question on the ASP.Net or MSDN forums.  Someone wanted to know how to use LINQ to sort by the day of the week and then by the time for a given datetime.  I did not have a chance to do anything with it, but for some reason it stuck in my head.  So, I came up with the answer, but of course I do not remember where the post was located.  The answer is very simple.

You use ThenBy.  What does that mean?  Well, you set your Order By clause up like you normally would but instead of using another Order By you just put a comma and add the next item you want to order by.

Here is a simple console app I wrote to demonstrate.  Keep in mind this is in C#.

class Program
    {
        static void Main(string[] args)
        {
            DateTime[] myDates = new DateTime[] {
                DateTime.Now.AddDays(-1).AddHours(-3),
                DateTime.Now.AddDays(-1).AddHours(-4),
                DateTime.Now.AddDays(-1).AddHours(-2),
                DateTime.Now.AddDays(-1).AddHours(-1),
                DateTime.Now.AddDays(-1),
                DateTime.Now.AddHours(-4),
                DateTime.Now.AddHours(-3),
                DateTime.Now.AddHours(-2),
                DateTime.Now.AddHours(-1)
            };

            var result = from d in myDates
                         orderby d.DayOfWeek.ToString(), d.ToLongTimeString()
                         select new { day = d.DayOfWeek.ToString(), time = d.ToLongTimeString() };

            foreach (var item in result.ToArray())
            {
                Console.WriteLine(String.Format("{0} at {1}", item.day, item.time));
            }
            /*
             * This is the output that I copy/pasted
            Monday at 1:24:02 PM
            Monday at 10:24:02 AM
            Monday at 11:24:02 AM
            Monday at 12:24:02 PM
            Monday at 9:24:02 AM
            Tuesday at 10:24:02 AM
            Tuesday at 11:24:02 AM
            Tuesday at 12:24:02 PM
            Tuesday at 9:24:02 AM
            */

            Console.ReadKey();
        }
    }

What I did is create an array of datetime objects called myDates.  To keep the day names and hours different you’ll notice I added some days and hours.  Next I do a LINQ query to obtain a new object that contains the day of the week and the long time string ordered first by the day of the week and then by the time. I store that in my result object. Finally to visualize what I have done I do a foreach loop through the result that I use as an array. As you can tell from the comment of the output it sorted first by the day of the week and then by the time.

Here is a great resource; LINQ Query Samples.

Advertisements

About SheldonS

Web developer for over 15 years mainly with Microsoft technologies from classic ASP to .NET 4. Husband, father, and aspiring amateur photographer.

Posted on October 1, 2013, in C#, LINQ and tagged , . Bookmark the permalink. Leave a comment.

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s

%d bloggers like this: