1
In my project I use the EF6
, use generic repositories and in my layer BLL
I have the class ClienteBLL
, which is a class that has the logical implementation methods of the class.
In one of the methods I return a list that receives all customers from the bank and then make an ordering
Method:
public IList<ClienteDTO> TodosClientes()
{
List<ClienteDTO> lista = clienteRepo.BuscarTodos().ToList();
lista.Sort(delegate (ClienteDTO a, ClienteDTO b) { return a.nome.CompareTo(b.nome); });
return lista;
}
My doubt: Is it wrong to order the list that way? If so, what do you recommend?
I found a much better way: How to Sort a List by a Property in the Object
So based on the link response I did so:
public IList<ClienteDTO> TodosClientes()
{
return clienteRepo.BuscarTodos().OrderBy(o => o.nome).ToList();
}
Now I have no doubt it’s right!
My generic repository:
public abstract class GenericRepository<T> : IDisposable, IGenericRepository<T> where T : class
{
GdPContext contexto = new GdPContext();
public IQueryable<T> BuscarTodos()
{
IQueryable<T> query = contexto.Set<T>();
return query;
}
public IQueryable<T> Buscar(Expression<Func<T, bool>> predicado)
{
IQueryable<T> query = contexto.Set<T>().Where(predicado);
return query;
}
public void Adicionar(T entity)
{
contexto.Set<T>().Add(entity);
}
public void Excluir(T entity)
{
contexto.Set<T>().Remove(entity);
}
public void Editar(T entity)
{
contexto.Set<T>().AddOrUpdate(entity);
}
public void Salvar()
{
contexto.SaveChanges();
}
public void Dispose()
{
contexto.Dispose();
}
}
The two ways you’re using are not recommended, because, you’re doing it wrong, you should do the
OrderBy
in a generic method in your repository class and use to sort in your SQL, what is happening is that you materialize the information and then sort in memory this causes low performance.– novic
Friend, if it is not asking a lot you have to explain better the part of ordering in SQL
– Márcio Sebastião
I need the implementation of your Repository, because then I can explain what could be done. In the explanation of SQL is why you before materializing the data (
ToList()
,First()
, etc) could add Orderby so that in your SQL is present the ordering and with the I reported not in memory.– novic
Got it! I edited the question and put the generic repository
– Márcio Sebastião