I am wondering if there is away to query a db set by the use of a variable in linq.
First of all I query my database to get the name of the table as a string, I want to convert this to a table name:
public static string getTableName()
{
string tableName = string.Empty;
var department= "Sales";
using (var context = new ALLDBEntities())
{
tableName = (from x in context.PROCESS_MATRIX
where x.AREA.Equals(product)
select x.ENTITY).FirstOrDefault();
}
return tableName;
}
Now I have my table name as a string, I want to be able to use this to write a linq query, but instead of using alldbEntities.sales
, I want to use table
. The reason for this is because I will need to write multiple queries to do the same thing, as I will have to query different tables.
Is there away to achieve this?
public List<sales> GetData(DateTime startDate, DateTime endDate)
{
var table = getTableName();
this.startDate = startDate.AddDays(-1);
this.endDate = endDate.AddDays(1);
using (var alldbEntities = new ALLDBEntities())
{
salesinfo = alldbEntities.sales.Where(f => f.Date >= this.startDate && f.Date <= this.endDate).ToList();
}
return salesinfo;
}