2
I need to pass two parameters to a generic function that returns database data and select only a few specific columns to display in one DataGridView
.
public List<TEntity> GetAll(Expression<Func<TEntity, bool>> Predicate,
Expression<Func<TEntity, TEntity>> Select)
{
var query = Context.Set<TEntity>()
.Where(Predicate)
.Select(Select).ToList();
return query;
}
For the consultation I will perform, I do not need to pass a Predicate
, I just need to pass a Select
to return to me:
PessoaId,
Login,
Senha,
UsuarioTipoId,
Descricao(UsuarioTipo),
PessoaTipoId,
Descricao(PessoaTipo).
If the User is a natural person, display the fields:
NomeCompleto,
Apelido,
DataNascimento
but if he is a legal person, display the fields:
RazaoSocial,
NomeFantasia,
DataAbertura
It is a rather complex expression. It is possible to do it and how to Pass it by parameter?
Below follow the classes:
//CLASSES MODEL
public class PessoaModel
{
public int PessoaId { get; set; }
public int PessoaTipoId { get; set; }
public DateTime DataInclusao { get; set; }
public virtual PessoaTipoModel PessoaTipo { get; set; }
public virtual PessoaFisicaModel PessoaFisica { get; set; }
public virtual PessoaJuridicaModel PessoaJuridica { get; set; }
}
public class UsuarioModel : PessoaModel
{
public string Login { get; set; }
public string Senha { get; set; }
public int UsuarioTipoId { get; set; }
public virtual UsuarioTipoModel UsuarioTipo { get; set; }
}
public class PessoaFisicaModel
{
public int PessoaId { get; set; }
public string NomeCompleto { get; set; }
public string Apelido { get; set; }
public DateTime? DataNascimento { get; set; }
public string CPF { get; set; }
public virtual PessoaModel Pessoa { get; set; }
}
public class PessoaJuridicaModel
{
public int PessoaId { get; set; }
public string RazaoSocial { get; set; }
public string NomeFantasia { get; set; }
public DateTime? DataAbertura { get; set; }
public string CNPJ { get; set; }
public virtual PessoaModel Pessoa { get; set; }
}
//CARREGAMENTO DO DATAGRIDVIEW
UsuarioRepository UsuarioRepositorio = new UsuarioRepository();
dgUsuarios.DataSource = UsuarioRepositorio.GetAll();
I needed something more or less like this (Passing parameters):
dgPesquisar.DataSource = UsuarioRepositorio.GetAll(null, u => u.login, u.Senha, u.NOmeCompleto );
you need to do on that
GetAll
only the Select expression and to define what the Person type is by the fieldPessoaTipoId
what is the value?– novic
failed to put the class code:
UsuarioTipoModel
andPessoaTipoModel
– novic
Personal ===> 1 P. Physical and 2 P. Legal....
– Master JR
Is it Personal or Personal? and lacks both classes
UsuarioTipoModel
andPessoaTipoModel
– novic
If your goal is to return the two types of people in your Getall(), with different lists (fields in select), this can’t be done, ie you can’t return a new Personal List and another new Person Juridicamodel at the same time.
– Marco Souza