Yes, your class needs to implement the interface IEnumerable
. In addition you have to make available the namespace System.Linq
.
You’re calling the class Pessoas
, this means that it will contain several people. So it seems to me that it makes sense to implement this interface. But if your intention is that this class represents only one person, there would not make sense and would have to store the lists of people in another object, maybe a list, as shown in the Carlosfigueira response. If your intention is to use a list to store people, then the list already implements the IEnumerable
and you would only need to use the namespace of LINQ. But if so, the class name gives a wrong indication of what it represents.
Anyway you can not call this method from the class itself, as in the example shown (I think). It has to be done with one instance. It may even be an instance, since C# allows variables to have names equal to types, but the name thus does not give a good indication of what it is. So you probably want it to be a var pessoas = new List<Pessoa>();
.
How LINQ is implemented over extension methods, these are only available when you do so explicitly. That is, LINQ methods will only be considered part of your object if they were made available with using
, otherwise they do not appear in Intellisense since they are not part of the object, they are auxiliary definitions apart and optional.
I found it curious that the other answers gave a solution using
List
. Is this what you want? I mean, you don’t want your own class to keep the list of people?– Maniero
That’s what I understood, since the question started with "I have a list
list<Pessoas>
".– carlosfigueira
@I don’t want my class to keep the list of people, I want to be able to order a
list<Pessoas>
, moving to avar
or to anotherlist<Pessoas>
.– MeuChapeu
I have already adapted my answer by giving more information to this case and by saying that the nomenclature you are using makes confusing what you are saving in the class. It is your discretion to use as you wish but this confusion shows how well-chosen names are easier to communicate intent.
– Maniero