I have the following methods to take from 5 different views a list of said view from EF Core 6.0 :
public List<Vw_Customers> GetCustomers(THEDBContext dbContext)
{
return dbContext.Vw_Customers.ToList();
}
public List<VW_Cars> GetCars(THEDBContext dbContext)
{
return dbContext.VW_Cars.ToList();
}
and so on...
It works, but I have to make "n" methods to take data from my database.
Is there a way where I can get the specific List passing the class of the view, like
public List<dynamic> GetData(THEDBContext dbContext, Class myViewClass)
{
return dbContext.myViewClass.ToList();
}
?
Searching around StackOverflow I found to use dbContext.Set<MyClass>
but it doesn't work with latest EF Core version.
You can do something like this
public class MyClass<T> where T : class
{
public List<T> GetData(THEDBContext dbContext)
{
return dbContext.Set<T>().ToList();
}
}
This is the generic way to return data from dbcontext, I assume you use 'dynamic' word to describe it will be something not fixed. Otherwise if you want to use dynamic type It is not genereic at all and I don't recomend it you to use in this case.
You should look some documentations and tutorials in order to understand how generic classes and methods works.