Recently useful to list handling of sorting and paging issues. Think about it or write a blog record. The following is implemented around the person class, with the person class having only name and age two properties
a .list<t> sort
1.1 List<t> offers a lot of sorting methods, sort (), order by (), OrderByDescending ().
Lstperson = lstperson.orderbydescending (x=>x.name). ToList ();//DescendingLstperson = Lstperson.orderby (x = x.age). ToList ();//Ascending//by name and age ascendingLstperson.sort ((x, y) = = { if((X.name.compareto (Y.name) >0) || (X.name = = y.name) && x.age >y.age)) { return 1; } Else if(X.name = = y.name) && (x.age = =y.age)) { return 0; } Else { return-1; } });
1.2 Because there's a recent Silverlight DataGrid that's like implementing the name of any column in a click, what do we do? Perhaps the first reaction is to think, for each attribute to write a sorting method does not have, in fact, such words inadvertently increase the amount of code, and not universal, in fact, here can be combined with reflection to achieve.
string " Name " = Lstperson.orderby (x = = X.gettype (). GetProperties (); return proinfos.where (info = info). Name = = propertityname). ToList () [0]. GetValue (x); }). ToList ();
Two .list<t> pagination
2.1 In Silverlight Sometimes we will get a lot of data from the backstage, stored in List<t>, but because the interface is limited can not be fully displayed, we will think of pagination display, for pagination display we basically the first idea is to set the size of each page by looping , the code is as follows:
/// <summary> ///get a single page of data/// </summary> /// <param name= "Lstperson" >Data Collection</param> /// <param name= "PageIndex" >page number (default starting from 1)</param> /// <param name= "PageSize" >number of data rows per page</param> /// <returns></returns> Public StaticList<person> GetPage (list<person> Lstperson,intPageIndex,intPageSize) {List<Person> Pageperson =NewList<person>(); for(intindex = (PageIndex-1) * PAGESIZE; Index < PageIndex * PageSize && index<lstperson.count; index++) {Pageperson.add (Lstperson[index]); } returnPageperson; }
2.2, in fact, LINQ has a skip and take method, skip means how many elements skipped, take to get a specific number of elements. It seems that the code is much simpler.
Public Static int int PageSize) { return1) * PageSize). Take (PageSize). ToList ();}
three,list<t> 's foreach usage.
2.1 How I do the same with each object in the list, which was previously traversed by a for loop, in fact LINQ provides a handy foreach implementation. Below I will be +2 for all the person ages.
2);
C # Basic----list<t> of LINQ