2

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;

  }
Jérôme Beau
  • 10,608
  • 5
  • 48
  • 52
kyorilys
  • 822
  • 13
  • 27
  • 1
    For the first: this is extension methods are for. For second: you looking something like "convert" string to `Func<>`, there are bunch of question of this topic on stackoverflow. (for example: [http://stackoverflow.com/questions/1707854/parse-string-to-c-sharp-lambda-func](http://stackoverflow.com/questions/1707854/parse-string-to-c-sharp-lambda-func)) – Fabio Nov 25 '16 at 08:24
  • what do you mean "are for"? – kyorilys Nov 25 '16 at 08:28
  • I mean you can create extension method for type `List` with name `SortByNameDesc`. – Fabio Nov 25 '16 at 08:30
  • 3
    The idea to `Keep().Chaining().Methods()` is called a _fluent interface_ (http://martinfowler.com/bliki/FluentInterface.html), and "adding" methods to types you don't own is done using _extension methods_ as others have already mentioned. – C.Evenhuis Nov 25 '16 at 08:35
  • Defenitively worth reading in this context: [Jon Skeet's Edulinq](https://codeblog.jonskeet.uk/category/edulinq) – Bill Tür stands with Ukraine Nov 25 '16 at 09:03

1 Answers1

3

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

Theodoros Chatzigiannakis
  • 28,773
  • 8
  • 68
  • 104