Click here to Skip to main content
15,888,454 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
At my previous project I have written such a method:

private static IQueryable<vw_gui_User> GetUsersOrderedQuery(IQueryable<vw_gui_User> query, string sortExpression, bool asc)
        {
            switch (sortExpression)
            {
                case "Username":
                    {
                        query = asc ? query.OrderBy(users => users.Username) :
                            query.OrderByDescending(users => users.Username);
                        break;
                    }
                case "email":
                    {
                        query = asc ? query.OrderBy(users => users.email) :
                            query.OrderByDescending(users => users.email);
                        break;
                    }
                case "Total":
                    {
                        query = asc ? query.OrderBy(users => users.Total) :
                            query.OrderByDescending(users => users.Total);
                        break;
                    }
                case "RealBalance":
                    {
                        query = asc ? query.OrderBy(users => users.RealBalance) :
                            query.OrderByDescending(users => users.RealBalance);
                        break;
                    }
                case "BonusMoney":
                    {
                        query = asc ? query.OrderBy(users => users.BonusMoney) :
                            query.OrderByDescending(users => users.BonusMoney);
                        break;
                    }
                case "PartnerProgram":
                    {
                        query = asc ? query.OrderBy(users => users.PartnerProgram) :
                            query.OrderByDescending(users => users.PartnerProgram);
                        break;
                    }
                case null:
                default:
                    sortExpression = null;
                    break;
            }

            if (sortExpression != null)
            {
                var ordered = ((IOrderedQueryable<vw_gui_User>)query);
                query = ordered.ThenBy(users => users.id);
            }
            else
                query = query.OrderBy(users => users.id);
            return query;
        }


What to do in this case to avoid many cases with string of class properties?

public List<Artefact> Select(int StartIndex, int PageSize,
           string SortExpression, bool SortDirection)
       {
           using (ZADataContext db = new ZADataContext())
           {
               ///How to implement this method without cases to process SortExpression?
           }
       }
Posted
Updated 6-Jul-10 0:43am
Comments
Christian Graus 6-Jul-10 18:40pm    
Why did you use a string for a sort expression, instead of an enum ? This is bad code, IMO, esp when some terms are capitalised and some are not. What if I sorted on "Email" or "bonusmoney" ?
Pavel Yermalovich 7-Jul-10 2:58am    
I use string as a sort expression because i need to sort almost all data in my application. Let's ignore the moment with capitalized letters. These are not essentials. You wanted to say that i need to create an enumeration for every class to implement sorting? So, please, give me the alternative decision without BAD code. Thanks in advice.

1 solution

Please check this[^]article. Sorting is implemented nicely with the help of reflection.
 
Share this answer
 

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



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900