0

I have a database first Entity Framework setup. Usually, I create procedures like this, to return a list of a defined object in my model.

    public List<Employees> GetEmployees()
    {
        var tables = _crmContext.Employees.ToList();

        return tables;
    }

That works fine but the case I can't figure out, it different. I want to return a list of an object in the entity framework model, based on a parameter passed to the procedure. I was trying something like this, but it's not working and I also don't know if is the right approach.

    public List<T> GetValuesFromTable(string table)
    {
        var results = _crmContext."table".ToList();

        return results;
    }

Thanks for the help.

1 Answer 1

1

I think the Generic Repository Pattern is what you're looking for. In this way, you can call all the tables you have given type with a single method. Example

Repository.cs

public class Repository<TEntity> : IRepository<TEntity> where TEntity : class
{
    protected readonly DbContext Context;

    public Repository(DbContext context)
    {
        this.Context = context;
    }

    public IEnumerable<TEntity> GetAll()
    {
        return Context.Set<TEntity>().ToList();
    }
 }

IRepository.cs

public interface IRepository<TEntity> where TEntity : class
{        
    IEnumerable<TEntity>GetAll();
}
Sign up to request clarification or add additional context in comments.

1 Comment

Thanks for answering :) I don't think I see this helping me. It must be that I don't get it right. I need to return all rows from a the table I am passing as a parameter. The context is already defined. What will change is the result depending on the specific table I ask.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.