Click here to Skip to main content
12,700,376 members (35,306 online)
Rate this:
Please Sign up or sign in to vote.
See more: ASP.NET .NET MVC .NET4
Hi everyone.
Recently i have encountered with such issue.
In my project i have a list of rooms (simple unordered set).

How can i implement ability of setting order in which this rooms will appears in second page?
Any thoughts?

The firs idea is to create a
, and perform appropriate actions under it.
But i'm not sure if it's a right way ....
Posted 14-Aug-12 3:08am
Updated 14-Aug-12 3:09am

1 solution

Rate this: bad
Please Sign up or sign in to vote.

Solution 1

To order a Queue:
var rooms = new Queue<Rooms>();

var orderByNumberAsc = rooms.OrderBy(r => r.Number);
var orderByNumberDesc = rooms.OrderByDescending(r => r.Number);

To dynamically order this based on string you need an extension:

public static IQueryable<T> OrderBy<T>(this IQueryable<T> items, string propertyName)
    var typeOfT = typeof(T);
    var parameter = Expression.Parameter(typeOfT, "parameter");
    var propertyType = typeOfT.GetProperty(propertyName).PropertyType;
    var propertyAccess = Expression.PropertyOrField(parameter, propertyName);
    var orderExpression = Expression.Lambda(propertyAccess, parameter);

    var expression = Expression.Call(typeof(Queryable), "OrderBy", new[] { typeOfT, propertyType }, items.Expression, Expression.Quote(orderExpression));
    return items.Provider.CreateQuery<T>(expression);

Then you can order by a string value:
var rooms = new Queue<Rooms>().AsQueryable();
var orderByNumberAsc = rooms.OrderBy("Number");

And you could easy create an extension for OrderByDescending(string propertyName)

Or a little less dynamic approach using switch statement:
switch (orderByParam)
    case "Number":
        rooms = rooms.OrderBy(r => r.Number);
    case "Capacity":
        rooms = rooms.OrderBy(r => r.Capacity);

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)

  Print Answers RSS
Top Experts
Last 24hrsThis month

Advertise | Privacy | Mobile
Web02 | 2.8.170118.1 | Last Updated 14 Aug 2012
Copyright © CodeProject, 1999-2017
All Rights Reserved. Terms of Service
Layout: fixed | fluid

CodeProject, 503-250 Ferrand Drive Toronto Ontario, M3C 3G8 Canada +1 416-849-8900 x 100