Delegate + lambda = auto Sort (i)

Source: Internet
Author: User

We can always see and use the ability to sort by fields, which is the effect of click Auto-Sorting in File Explorer:

To implement this function, you must first make the following settings in the HTML code of the interface:

That is: Set the field to sort: The sortable property, that is, set the field to be sortable.

Since it is the interface of the sort, there must be a parameter, to the background to the corresponding method to return data to the interface, then there are two methods:

1. The field to be sorted as a parameter, pass the layer, to the background to fetch the method:

<span style= "FONT-SIZE:18PX;" >   //Get sort, if sort is empty, the default sort field is "entityname", otherwise the field is obtained.   string sort = request["sort"] = = null? "EntityName": convert.tostring (request["sort"]. ToString ());   Order similarly: The default sort is ascending   string order = request["order"] = = null? "ASC": Convert.ToString (request["Order"]);</span>
pass the two parameter layers, and finally use the underlying Loadpageitems method:

PageSize: The number of data bars displayed per page; PageIndex: Page number

Total: Quantity

WHERELAMDBA: Query condition (form of lambda expression)

ORDERBYLAMBDA: Sorting criteria

ISASC: Whether it is ascending

<span style= "FONT-SIZE:18PX;" >     iqueryable<t> loadpageitems<tkey> (int pageSize, int pageIndex, out int total, expression<func <t, bool>> Wherelambda, func<t, tkey> Orderbylambda, bool isasc);</span>
one of the WHERELAMDA: said Simple is a SQL statement with an order by, all the required parameters and conditions are available, you can have a purposeful query to the data in the database, and finally the data layer of return to the interface layer, presented to the user.

2. Once all the data has been queried, it is not displayed before the interface, that is, the data is sorted directly in the controller:

<span style= "FONT-SIZE:18PX;" >//<summary>//Query data-sort//</summary>//<param name= "EntityName"        > Entity name </param>//<returns></returns> public Jsonresult querydata (string strcondition) {//The number of bars in the page int pageSize = Int32.Parse (request.params["Rows"].            ToString ()); The number of pages to load, the first page int pageIndex = int32.parse (request.params["page"].            ToString ());            int total = 0; #region query based on query criteria, try {list<nonquerypropertiesviewmodel> ltprop = NONQUERYPR                Opertiesservicewcf.getallnonqueryproperties (PageSize, PageIndex, out Total, strcondition); Instantiating another object, hosting a collection of Ltprop.orderby,//And then header stitching on the data in Ltagain//Get sort, if sort is empty, the default sort field is "entit                Yname ", otherwise gets to the field. string sort = request["sort"] = = null? "EntityName": convert.tostring (request["sort"].   ToString ());             String order = request["order"] = = null?                "ASC": Convert.ToString (request["Order"]); list<nonquerypropertiesviewmodel> Ltagain = Ltprop.orderby (U = U.gettype (). GetProperty (sort). GetValue (U, NULL)).                ToList ();                Collection re-integration, splicing table title var ltpropview = (p = new                    {P.nonqueryid, p.entityname, P.entitydesc, P.propertyname, P.propertydesc, P.controls.controlid, Ntrols.                     Controldesc, P.controlhtmlid, P.propertyorder, P.controlhtmlname, P.isshow, p.isnecessary}).                ToList (); Convert to JSON, pass to Easyui-datagrid//stitching JSON format//define return value var strJSON = new {total, rows = Ltpropview};            Return Json (Strjson, jsonrequestbehavior.allowget);            } catch (Exception e) {throw e; } #endregion} #endregion </span>
using this method, we directly borrow the order by property of the generic collection, sort the data according to the sort criteria and sort, and then assign the data in the Ltagain to the interface, so that when the interface is loaded for the first time, The data that is displayed is the default sort field in EntityName. When you click on another field, the data will be implemented accordingly.

Although we have two choices, programming is always about efficiency, which is the sensitivity of the program response. So let's analyze the above two methods:

The first method is the most easy to think of, but in the cooperative development must have a Loadpageitems method, in addition, each time you click a different field to sort, the entire process of acquiring data to be re-executed, not only wasted space, but also reduced the execution efficiency of the program.

The second method is sorted on the basis of querying data, and the method of querying data is reused, which reduces the re-development of the underlying method, and makes each sort as long as it is reordered in the controller, which improves efficiency and saves time.

To sum up, development is not only the use of existing knowledge, more importantly, the use of existing resources skillfully, improve efficiency.

Delegate + lambda = auto Sort (i)

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: 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.