简体   繁体   中英

what is name of the List<T> design pattern called? how to implement it

As we know that the List for C# is possible to put the function behind but do the function to filter the list.

For example, Assume that the many count inside the students List.

var students=new List<student>();

Then to get the list of student we can add some function behind like

students.find(id).sortbydesending(k=>k.x).skip(10).take(5).toList();

the sequence of putting the skip() and take() will effect the result.

I was doing the coding on an method to retrieve the list of student accordingly.

First , I want to do something similar with the list function. For example i need order the list Of Student According student name.

instead of doing

students.sortbydescending(k=>k.name).toList();

I want code is like

students.sortbyNamedesc().toList();

and it will return the same result as above.

Second is the design pattern name and (If possible) implementation guide

This is because I plan to do something like this.

getStudent().searchName(searchQuery).sortby(id);

Then i can get student name similar with search query and sort it by the student id instead of

getstudent(searchQuery,id,skip,take);

public IList<Student> getStudent(searchQuery,id,skip,take){
   var students=new List<student>();
   if(searchquery!="")
        students.where(x=>x.x==searchquery);

   if(skip!=null&&skip!=0)
        students.skip(skip);

   if(take!=null&&take!=0)
     students.take(take);

    return students;

  }

These are called extension methods . You define them as static methods in static classes, with a this before the first parameter. For example:

public static class StudentExtensions
{
    public static IEnumerable<Student> OrderByNameDescending(this IEnumerable<Student> source)
    {
        return source.OrderByDescending(x => x.Name);
    }
}

Usage:

studentList.OrderByNameDescending()

(The static class must be accessible at the point of usage and you must be in its namespace, or include it with a using statement.)

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM