I have a list that contains dates that are in GMT format.
What is the most elegant way to ensure that the following Lambda expression orders on the date field as GMT?
ProductsList.OrderBy(Product => Product.Added).ToList();
Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.
Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.
Lost your password? Please enter your email address. You will receive a link and will create a new password via email.
Please briefly explain why you feel this question should be reported.
Please briefly explain why you feel this answer should be reported.
Please briefly explain why you feel this user should be reported.
The inbuilt LINQ operators use the expected sort operations (for LINQ-to-Objects, it does this using
Comparer<T>.Default. Your lambda expression is strongly typed; behind the scenes the compiler inferred some generics for you – it is actually:It already knows it is a
DateTime, so the only time you’d need to do something extra here is if your dates contain a range of different timezones. Of course, within a timezone you should be fine, but you could (if you were paranoid) convert all to UTC – I don’t think you need to do this in your case, but:Note that this actually creates a second list (it doesn’t change the ordering of the original list); you can use the code from here to do an in-place sort using lambdas: