Construction of ASP.net mvc4+ef5+easyui+unity2.x injection background Management system (17)

Source: Internet
Author: User

LINQ Dynamic Sorting

Fix a bug in the program first the bug in the Gridpager class, Sord the name of the sort to fill in the error, causing the background to be unable to get the DataGrid sort field

There was no such story, in order to simplify the 20 lines of code into 2 lines, I have consulted some information, borrowed some great God's suggestions, first of all, thanks to the eighth lecture, 11 floor @nyth and 15 floor @ Beauty Drunk, Emperor's proposal delivery, each of the sort to be a judge it is really troublesome, We use reflection to solve this problem.

First look at the original code

Sort if (Pager.order = = "desc") {switch (Pager.order) {
                        Case "Createtime": Querydata = querydata.orderbydescending (c => c.createtime);
                    Break
                        Case "Name": Querydata = querydata.orderbydescending (c => c.name);
                    Break
                        Default:querydata = querydata.orderbydescending (c => c.createtime);
                Break
                    } else {switch (Pager.order) {
                        Case "Createtime": Querydata = Querydata.orderby (c => c.createtime);
                    Break
                        Case "Name": Querydata = Querydata.orderby (c => c.name);
                    Break
   Default                     Querydata = Querydata.orderby (c => c.createtime);
                Break }
            }

Each of the above order to be judged, the use of reflection can solve the problem, I sealed him up. (need to have some understanding of reflection)

The new Linqhelper class code in App.common is as follows (there are 3 methods below, I have commented on the method, I can see it)

Using System;
Using System.Collections.Generic;
Using System.Linq;
Using System.Linq.Expressions;
Using System.Reflection;
Using System.Text;
    
Using System.Threading.Tasks; Namespace App.common {public class Linqhelper {///<summary>///sorting///
       T  <typeparam name= "T" ></typeparam>///<param name= "source" ></param>///<param Name= "SortExpression" ></param>///<param name= "SortDirection" ></param>///<return s></returns> public static iqueryable<t> datasorting<t> (iqueryable<t> source, string so Rtexpression, String sortdirection) {string sortingdir = string.
            Empty; if (Sortdirection.toupper ().
            Trim () = = "ASC") Sortingdir = "by"; else if (Sortdirection.toupper ().
            Trim () = = "DESC") Sortingdir = "OrderByDescending"; ParametereXpression param = Expression.parameter (typeof (T), sortexpression); PropertyInfo pi = typeof (T).
            GetProperty (SortExpression);
            type[] types = new type[2];
            Types[0] = typeof (T); TYPES[1] = pi.
            PropertyType; Expression expr = Expression.call (typeof (Queryable), Sortingdir, types, source.
            Expression, Expression.lambda (Expression.property (param, sortexpression), param)); iqueryable<t> query = source. AsQueryable ().
            Provider.createquery<t> (expr);
        return query; }///<summary>///paging///</summary>///<typeparam name= "T" ></typeparam
       >///<param name= "source" ></param>///<param name= "PageNumber" ></param> <param name= "PageSize" ></param>///<returns></returns> public static IQUERYABL E<t> datapaging<t> (iqueryable<t> source, int pagenumber, int PageSize) {if (pagenumber <= 1) {return source.
            Take (pageSize); else {return source. Skip ((pageNumber-1) * pageSize).
            Take (pageSize); ///<summary>///Sorting and paging///</summary>///<typeparam name= "T" ;</typeparam>///<param name= "source" ></param>///<param name= "SortExpression" >&lt ;/param>///<param name= "SortDirection" ></param>///<param name= "PageNumber" ></par am>///<param name= "pageSize" ></param>///<returns></returns> Public St atic iqueryable<t> sortingandpaging<t> (iqueryable<t> source, string sortexpression, String SortDirection, int pagenumber, int pageSize) {iqueryable<t> query = datasorting<t> (source , SortExpression, Sortdirection);
        return datapaging (query, pagenumber, pageSize); }
    }
}

See more highlights of this column: http://www.bianceng.cnhttp://www.bianceng.cn/webkf/aspx/

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.