I have this function that shows a list of messages in reverse order.
protected void setupMessages(IList<Message> Messages)
{
List<Message> messages = new List<Message>() ;
messages.AddRange( Messages.OrderBy(message => message.DateAdded).Reverse());
MessagesRepeater.DataSource = messages;
MessagesRepeater.DataBind();
}
I was wondering if there was a way to reverse the order within the lambda query without calling the Reverse Method? Or is calling Reverse() the only way to do this?
You simply want to use the
OrderByDescendingextension method, as opposed toOrderBy.Indeed, this would be a more efficient method, since it only requires one iteration of the collection rather than two.