OrderBy in T lists

4

I have a list list<Pessoas> :

My class people:

public class Pessoas
{
    public String Nome { get; set; }
    public DateTime Nascimento { get; set; }
}

Theoretically, I could make a OrderBy like this:

Pessoas.OrderBy(p => p.Nome);

But OrderBy does not appear to do this. Does my class have to 'be' or 'implement' anything from IEnumerable ?

    
asked by anonymous 31.03.2015 / 19:13

4 answers

4

Yes, your class needs to implement the IEnumerable interface. In addition, you have to make the namespace System.Linq .

You are calling the Pessoas class. This means that it will contain several people. So it makes sense to implement this interface. But if your intention is that this class represents only one person, then it would not make sense and would have to save the lists of people on another object, perhaps a list, as shown in the answer of the carlosfigueira. If you intend to use a list to save people, then the list already implements IEnumerable and you only need to use LINQ's namespace . But if it is, 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 an instance. It may even be an instance, since C # allows variables to have names that are the same as the types, but the name does not give a good indication of what it is. So you probably want a var pessoas = new List<Pessoa>(); .

How LINQ is implemented over extension methods , these are only available when you explicitly do so. That is, LINQ methods will only be considered part of their 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. >     

31.03.2015 / 19:17
5

OrderBy is an extension method in the namespace% with%. If you have it in your System.Linq s list, then the method should appear.

using System;
using System.Collections.Generic;
using System.Linq;

public class Pessoas {
    public String Nome { get; set; }
    public DateTime Nascimento { get; set; }
}

public class Teste {
    public static void Main() {
        List<Pessoas> lista = new List<Pessoas>();
        lista.Add(new Pessoas { Nome = "Maria", Nascimento = DateTime.Today });
        lista.Add(new Pessoas { Nome = "Jose", Nascimento = DateTime.Today });
        var ordenada = lista.OrderBy(p => p.Nome);
        foreach (var pessoa in ordenada) {
            Console.WriteLine(pessoa.Nome);
        }
    }
}
    
31.03.2015 / 19:16
3

If you are using the form below all you need to do is add a using System.Linq at the beginning of the file.

public class Pessoas
{
    public String Nome { get; set; }
    public DateTime Nascimento { get; set; }
}

var Pessoas = new List<Pessoas>();

Pessoas.OrderBy(x => x.Nome);

If you are using it, you only need the right%%.

    
31.03.2015 / 19:23
0
var lOrdenada = lPessoas.OrderBy(p => p.Nome);

ou

var lOrdenada2 = (from pessoa in lPessoas orderby pessoa.Nome select pessoa).ToList();
    
31.03.2015 / 19:53