Pass a lambda statement per parameter - C # - Entity

2

I have a method in which I need to pass only one select per parameter:

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

I need to pass a select that brings me the following fields: UsuarioId, Login, Senha, UsuarioTipoId e Descricao . It would look something like this:

dgPesquisar.DataSource = UsuarioRepositorio.GetAll(u => new 
    { 
        x.UsuarioId, 
        x.Login, 
        x.Senha, 
        x.UsuarioTipoId, 
        x.Descricao
     });

Is it necessary to do some Join in the tables?

Would anyone know how to help me assemble the expression and how to pass it by parameter?

//Classes:
public class UsuarioTipoModel
{
    public int UsuarioTipoId { get; set; }
    public string Descricao { get; set; }

    public virtual ICollection<UsuarioModel> Usuario { 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; }

}

Repository

public abstract class BaseRepository<TEntity> : Interfaces.IRepository<TEntity> where TEntity : class
{
    private DbContext Context = null;
    protected DbSet<TEntity> Entity = null;

    public BaseRepository(DbContext Context)
    {
        this.Context = Context;
        this.Entity = Context.Set<TEntity>();
    }

    public void Add(TEntity Entity)
    {
        Context.Set<TEntity>().Add(Entity);
        Context.SaveChanges();
    }

    public void Delete(TEntity Entity)
    {
        Context.Set<TEntity>().Remove(Entity);
        Context.SaveChanges();
    }

    public TEntity Find(int Id)
    {
        var query = Context.Set<TEntity>().Find(Id);
        return query;
    }

    public List<TEntity> GetAll()
    {
        var query = Context.Set<TEntity>().ToList();
        return query;
    }

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

    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;
    }

    public void Update(TEntity Entity)
    {
        Context.Entry(Entity).State = EntityState.Modified;
        Context.SaveChanges();
    }
}
    
asked by anonymous 07.11.2016 / 23:52

1 answer

1

In order to be able to say with more certainty what the code in your context would look like, I'm going to need the full User repository class. But below is an idea of how the declaration of the method and its use can be made.

The declaration of the method would be as follows:

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

The method call would look like this:

UsuarioRetornoModel usuarioRetorno = UsuarioRepositorio.GetAll<UsuarioRetornoModel>(u => new UsuarioRetornoModel 
    { 
        UsuarioId = u.UsuarioId, 
        Login = u.Login, 
        Senha = u.Senha, 
        UsuarioTipoId = u.UsuarioTipo.UsuarioTipoId, 
        UsuarioTipoDescricao = u.UsuarioTipo.Descricao
     }
);

The return class:

public class UsuarioRetornoModel
{
    public int UsuarioId { get; set; }
    public string Login { get; set; }
    public string Senha { get; set; }
    public int UsuarioTipoId { get; set; }
    public string UsuarioTipoDescricao { get; set; }
}

Updated

The properties UsuarioId , Login , Senha , UsuarioTipoId , and UsuarioTipoDescricao were considered properties of the class ReturnUserModel and u within the expression lambda would be related to the user's entity. To access properties of objects that come from a foreign key relationship, you must use include. The property name was passed inside the Include("UsuarioTipo") .

Updated II

Make sure you implement the method in the interface.

public interface IRepository<TEntity>
{
    ...
    List<TResult> GetAll<TResult>(Expression<Func<TEntity, TResult>> select);
    ...
}
    
08.11.2016 / 00:57