Build a back-end management system for ASP.-LINQ Dynamic sequencing (mvc4+ef5+easyui+unity2.x)

Source: Internet
Author: User

First fix a bug in the program this bug in the Gridpager class, change Sord to sort the name fill error, will cause the background has been unable to get the DataGrid's sort field

Originally there is no such talk, in order to make 20 lines of code to streamline into 2 lines, I looked up some information, draw on some of the Great God's advice, first of all thanks to the eighth, 11 floor @nyth and 15 floor @ Beauty Drunk, the proposal of the Emperor, each order to be a judge this 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 these orders should be judged, using reflection to solve the problem, I have encapsulated him. (need to have some understanding of reflection)

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

usingSystem;usingSystem.Collections.Generic;usingSystem.Linq;usingSystem.Linq.Expressions;usingSystem.Reflection;usingSystem.Text;usingSystem.Threading.Tasks;namespaceapp.common{ Public classLinqhelper {/// <summary>       ///Sort/// </summary>       /// <typeparam name= "T" ></typeparam>       /// <param name= "source" ></param>       /// <param name= "SortExpression" ></param>       /// <param name= "SortDirection" ></param>       /// <returns></returns>         Public StaticIqueryable<t> datasorting<t> (iqueryable<t> source,stringSortExpression,stringsortdirection) {            stringSortingdir =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=Newtype[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); returnquery; }       /// <summary>       ///page Out/// </summary>       /// <typeparam name= "T" ></typeparam>       /// <param name= "source" ></param>       /// <param name= "pagenumber" ></param>       /// <param name= "pageSize" ></param>       /// <returns></returns>         Public StaticIqueryable<t> datapaging<t> (iqueryable<t> source,intPageNumber,intpageSize) {            if(PageNumber <=1)            {                returnsource.            Take (pageSize); }            Else            {                returnSource. Skip ((PageNumber-1) *pageSize).            Take (pageSize); }        }       /// <summary>        ///sort and page out/// </summary>       /// <typeparam name= "T" ></typeparam>       /// <param name= "source" ></param>       /// <param name= "SortExpression" ></param>       /// <param name= "SortDirection" ></param>       /// <param name= "pagenumber" ></param>       /// <param name= "pageSize" ></param>       /// <returns></returns>         Public StaticIqueryable<t> sortingandpaging<t> (iqueryable<t> source,stringSortExpression,stringSortDirection,intPageNumber,intpageSize) {IQueryable<T> query = datasorting<t>(source, SortExpression, sortdirection); returndatapaging (query, pagenumber, pageSize); }    }}
View Code

Our code analysis for sorting is as follows:

ParameterExpression param = Expression.parameter (typeof (T), sortexpression);

The types we pass in Find the fields we need to sort.
PropertyInfo pi = typeof (T). GetProperty (SortExpression);

Remove the related properties of the field you want to sort

type[] types = new type[2];
Types[0] = typeof (T); Gets the type of the dataset to be sorted.
TYPES[1] = pi.  PropertyType; Take out what type of field we sort (that is, the value type passed in the parameter)

Expression expr = Expression.call (typeof (Queryable), Sortingdir, types, source. Expression, Expression.lambda (Expression.property (param, sortexpression), param));

To build a sort expression
iqueryable<t> query = source. AsQueryable (). Provider.createquery<t> (expr);

Perform sort

Now let's modify the Syssample's BLL layer.

Change the code above to

Querydata = linqhelper.datasorting (Querydata,pager.sort,pager.order);

Our code suddenly streamlined into a line, like a bar.

Build a back-end management system for ASP.-LINQ Dynamic sequencing (mvc4+ef5+easyui+unity2.x)

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.