Click here to Skip to main content
Rate this: bad
good
Please Sign up or sign in to vote.
See more: ASP.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
Queue<Rooms>
, and perform appropriate actions under it.
But i'm not sure if it's a right way ....
Posted 14-Aug-12 2:08am
Edited 14-Aug-12 2:09am
v2

1 solution

Rate this: bad
good
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);
        break;
    case "Capacity":
        rooms = rooms.OrderBy(r => r.Capacity);
        break;
}
  Permalink  

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

  Print Answers RSS
0 OriginalGriff 490
1 Gihan Liyanage 338
2 ChauhanAjay 180
3 Vinay Mistry 160
4 Sergey Alexandrovich Kryukov 138
0 Sergey Alexandrovich Kryukov 9,011
1 OriginalGriff 7,941
2 CPallini 2,603
3 Richard MacCutchan 2,121
4 Abhinav S 1,928


Advertise | Privacy | Mobile
Web02 | 2.8.140827.1 | Last Updated 14 Aug 2012
Copyright © CodeProject, 1999-2014
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