Delegate + Lambda = automatic sort (1), delegate Lambda sort

Source: Internet
Author: User

Delegate + Lambda = automatic sort (1), delegate Lambda sort

We can always see and use the field sorting function, that is, the effect of clicking Automatic Sorting in the file resource manager:

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

That is, set the sortable attribute for the field to be sorted, that is, set the field to be sorted.

Since the interface is sorted, there must be a parameter, and the corresponding method is retrieved from the background to return data to the interface, there are two methods:

1. The fields to be sorted are passed as parameters and retrieved in the background using the following method:

<Span style = "font-size: 18px;"> // If sort is null, the default sorting field is "entityName". Otherwise, the obtained field is used. String sort = Request ["sort"] = null? "EntityName": Convert. toString (Request ["sort"]. toString (); // same as order: the default sorting method is ascending string order = Request ["order"] = null? "Asc": Convert. ToString (Request ["order"]); </span>
Pass the two parameters layer by layer, and finally use the underlying LoadpageItems method:

// PageSize: the number of data entries displayed on each page; pageIndex: page number

// Total: total

// Wherelamdba: Query condition (lambda expression form)

// OrderbyLambda: Sorting Condition

// IsAsc: whether it is in ascending order

<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>
WhereLamda: Simply put, it is an SQL statement with order by. After all the required parameters and conditions are met, You can query the data in the database for a purpose, finally, the data is returned to the interface layer and presented to the user.

2. After all the data is queried and the interface is not displayed, sort the data 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) {// number of page entries int pageSize = Int32.Parse (Request. params ["rows"]. toString (); // The number of pages to load. The number of pages int pageIndex = Int32.Parse (Request. params ["page"]. toString (); int total = 0; # region queries try {List <NonQueryProperti Based on query Conditions EsViewModel> ltProp = nonQueryPropertiesServiceWCF. getAllNonQueryProperties (pageSize, pageIndex, out total, strCondition); // instantiate another object and host the ltProp. the set of orderBy, // and then splice the table header in ltAgain. // If sort is empty, the sorting field is "entityName" by default ", otherwise, it is the obtained 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 (); // reintegrate the set and splice the table title var ltPropView = ltAgain. select (p => new {p. nonQueryId, p. entityName, p. entityDesc, p. propertyName, p. propertyDesc, p. controls. controlId, p. controls. controlDesc, p. controlHtmlId, p. propertyOrder, p. controlHtmlName, p. isShow, p. isNecessary }). toList (); // convert to JSON and pass it to easyui-datagrid // splice 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 use the order By attribute of the generic set to sort data according to the sorting conditions and sorting methods, then, the sorted data, that is, the data in LtAgain, is assigned to the interface. When the interface is loaded for the first time, the data in the default sorting field is displayed with EntityName. When you click another field, the data will be operated accordingly.

Although we have two options, we have always focused on efficiency in programming, that is, the sensitivity of program response. Let's analyze the above two methods:

The first method is the easiest way to think of, but in cooperative development, the LoadpageItems method must be available at the underlying layer. In addition, each time you click different fields for sorting, the entire process of obtaining data must be re-executed, it not only wastes space, but also reduces program execution efficiency.

The second method sorts data based on the query data, and the data query method is used again, which not only reduces the re-development of the underlying method, but also enables each sort to be re-ordered in the controller, this improves efficiency and saves time.

To sum up, development is not only about the use of existing knowledge, but more importantly, clever use of existing resources to improve efficiency.

Related Article

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.