Best way to recover data for a Datagridview?

Asked

Viewed 305 times

3

What is the best way to recover data from a direct table to a Datagridview ?

I have several doubts, because, many people do not recommend using Unitofwork which makes it much easier and leaves the code much cleaner.

    var unitOfWork = new UnitOfWork(new DBConecta());
    var _registros = unitOfWork.RepoPaises.Get();
    BindingListView<CNAES> _DataView = new BindingListView<CNAES>(_registros);
    dbGride.DataSource = _DataView;

After seeing here in the forum people saying that it does not make sense to use Uow, since the Entity Framework is already the access layer, I went back to the normal mode, but I’m finding it very strange

        _context = new DBConecta();

        var registros = _context.CNAES.
            Select(c => new
            {
                id = c.id,
                nome = c.nome,
                ativo = c.ativo,
                usuarioNome = c.usuario.primeiro_nome + " " + c.usuario.segundo_nome,
                created_at = c.created_at,
                updated_at = c.updated_at,
                usuario_id = c.usuario_id
            }).ToList();


        this.dbGride.DataSource = registros;
        AfterRetrieve();
        _context.Dispose();

As I have never worked in . NET teams do not know what the best practices, I would like an orientation. Thank you.

inserir a descrição da imagem aqui

  • With this amount of data the best way is the Entity Framework.

2 answers

0

The way you did is not wrong.

As a suggestion, in your CNAES entity you could create a property called userName that returns the name and surname

public usuarioNome
{
    get {
        if (usuario == null) return "";
        return usuario.primeiro_nome + " " + usuario.segundo_nome
    }
}

So vc can simplify your code to popular your combo dbGride.DataSource = _context.CNAES.ToList();

A good practice would also be to paginate your data using Take and Skip methods

dbGride.DataSource = _context.CNAES.Skip(100).Take(50).ToList();
  • In fact, I was already doing this, but this generates a problem, Datagridview is running this Get all the time.

  • Paul, another problem is that in this way, all user table fields are retrieved http://pastebin.com/ybkSDgzz

  • @duardbr is not wrong to run this get for each line... that’s right because it’s a "calculated" field and you keep the rule in your domain...

  • the problem, is that this select also brings ALL FIELDS of the user table, and I wanted only the calculated name.

0

Good people, the best way to make this select, involving an entity and the user table (to fetch the user who worked last on the record) that I found, was this :

    protected override void Retrieve()
    {
        base.Retrieve();
        _context = new DBConecta(this.strConecta);

        var _query = from r in _context.CNAES
                     from u in _context.Usuarios
                     where u.id == r.usuario_id
                     select new { r.id, r.nome, r.ativo, r.created_at, r.updated_at, r.usuario_id, usuarioNome = r.usuario.login };

        dbGride.DataSource = _query.ToList();
        AfterRetrieve();
    }

Doing so, it does not bring all the user table fields but only the field I asked for. This is very important since a large table, bring all data from another referenced table can have a cost !

SELECT

Extent1.id, Extent1.nome, Extent1.ativo, Extent1.created_at, Extent1.updated_at, Extent1.usuario_id, Extent2.primeiro_nome FROM cnaes AS Extent1 INNER JOIN Usuarios AS Extent2 ON Extent1.usuario_id = Extent2.id -- Executing at 13/03/2016 16:54:36 -03:00 -- Completed in 0 ms with result: Efmysqldatareader

For me this was the best solution.

Browser other questions tagged

You are not signed in. Login or sign up in order to post.