Modify generic method to merge information into classes

0

I am a first-time sailor with EntityFramework and am working with a USUARIO class that inherits from PESSOA .

There are other classes like PESSOATIPO , PESSOAFISICA , PESSOAJURIDICA , etc., the problem is that when I load my DataGridView , EntityFramework can not separate the data from each table and position the fields correctly inside of the lines. See how PESSOAFISICA should be displayed the fields NOMECOMPLETO , APELIDO E DATANASCIMENTO .

  

This is because I have a generic function that returns everything to me. I would like to know if you can implement / change the function below to pass some kind of select ( Lambda ) to it to return columns in the order I want, so DataGridview loaded correctly and the load has a good performance?

Example:

Usuario.Select(x=> x.USuarioId, 
               x.PessoaFisica.NomeCompleto, 
               x.PessoaFisica.Apelido, 
               x.PessoaFisica.DataNascimento).

or something like that.

public List<TEntity> GetAll(Expression<Func<TEntity, bool>> Predicate)
{
    var query = Context.Set<TEntity>().Where(Predicate).ToList();
    return query;
}
    
asked by anonymous 06.11.2016 / 15:00

1 answer

0

Yes, there is a way to bring you a list with type created at runtime ( a class that represents this new data model can also be created), create a method with the code below, where TResult is a new data type that will be created in the output and can assign the new list in DataGridView

public List<TResult> GetAll<TResult>(Expression<Func<TEntity, bool>> predicate, 
                                     Expression<Func<TEntity, TResult>> select)
{
    var query = Context.Set<TEntity>()
                       .Where(predicate)
                       .Select(select) 
                       .ToList();
    return query;
}

In the method call it would be:

.GetAll(x => x.UsuarioId == 1, s = new {
        s.UsuarioId, ... //e continua colocando os dados da relação e precisa
});

Note: I believe you should implement the method in interface to propagate in the classes that implement it.

References:

06.11.2016 / 15:18