ProfK
ProfK

Reputation: 51063

Dynamic 'table' name in LINQ to entities

I need to select from varying levels of reporting department tables, i.e. Dept1, Dept2, Dept3, ect. [1] depending on which reporting level the user chooses. How can I dynamically express the 'table' to select from based on a given string parameter, which is the table name?

[1] Easy points (lets talk) for anyone that can help me out of this, deeper mess than the question exposed.

BREAKING NEWS: Looks like I'm going with a 'switch/case' construct. I'm tired of poring over idiotic non-solutions, and the first intelligent one I found doesn't work easily enought (without generations monastic, secluded dedication). The customer's needs come before mine.

Upvotes: 2

Views: 1340

Answers (2)

Stephen Jennings
Stephen Jennings

Reputation: 13234

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 Department table which defines a department name and its ID. Then, you can have a single table with the entities you are searching for and a foreign key pointing to a department.

So, if the entities 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 query might look something like this:

var Employees = from emp in db.Employee.Include("Department")
                where emp.Department.Name == "Sales"
                select emp;

Upvotes: 0

Quintin Robinson
Quintin Robinson

Reputation: 82325

I don't believe the EF has an equivalent of Linq->SQL's GetTable yet although I did find this post about a possibly future implementation of GetEntitySet as well as a simple implementation you can use I would check it out.

Upvotes: 1

Related Questions