如何通过排序有效地实现DAL

如何通过排序有效地实现DAL

本文介绍了如何通过排序有效地实现DAL?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

在上一个项目中,我编写了这样的方法:

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?
           }
       }

推荐答案


这篇关于如何通过排序有效地实现DAL?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

09-09 18:16