Dynamic table name in LINQ for objects
I need to choose from different levels of reporting department tables i.e. Dept1, Dept2, Dept3, ect. [1] depending on what level of reporting the user chooses. How can I dynamically express a "table" to select based on a given string parameter which is the name of the table?
[1] Easy points (let's talk) for those who can help me with this, a deeper mess than the subject question.
BREAKING NEWS: Looks like I'm going to build a "switch / case" construct. I'm tired of looking at idiotic non-solutions, and the first smart one I found doesn't work easily enough (no generation of monastic, solitary devotion). The client's needs reach mine.
source to share
Do you have the ability to change the schema? I suspect you are asking this question because you don't, but if you do, it might be easier to have a table Department
that defines the department name and its ID. Then you can have one table with the entities you are looking for and a foreign key pointing to the department.
So, if the objects you are trying to query are employees, your tables might look like this:
Department
------------
ID int
Name text
Employee
--------------------
ID int
Name text
DepartmentID int
Then your request might look something like this:
var Employees = from emp in db.Employee.Include("Department")
where emp.Department.Name == "Sales"
select emp;
source to share