Choose table dynamically in EF
-
For my client's sake, I have forgone pride and dignity and opted for the exampled means of choosing an entityset to select from, but I need to join this to another, fixed, entityset, and this challenge is beyond me today. I need to project a set of attributes from both Department*n,* and another entity set called Sites. I can join Department1 and Sites, but I am stumped at joining oq to Sites to select a projection of fields from both. Must I prostitute myself further, and repeat the entire query in each case of the switch statement?
case "Department1": var b = from d1 in ents.Department1 join w in ents.Sites on d1.Sites.SiteId equals w.SiteId select d1; selectedDepartments.DataSource = b; break; case "Department2": var c = from d1 in ents.Department2 join w in ents.Sites on d1.Sites.SiteId equals w.SiteId select d1; break; case "Department3":
-
For my client's sake, I have forgone pride and dignity and opted for the exampled means of choosing an entityset to select from, but I need to join this to another, fixed, entityset, and this challenge is beyond me today. I need to project a set of attributes from both Department*n,* and another entity set called Sites. I can join Department1 and Sites, but I am stumped at joining oq to Sites to select a projection of fields from both. Must I prostitute myself further, and repeat the entire query in each case of the switch statement?
case "Department1": var b = from d1 in ents.Department1 join w in ents.Sites on d1.Sites.SiteId equals w.SiteId select d1; selectedDepartments.DataSource = b; break; case "Department2": var c = from d1 in ents.Department2 join w in ents.Sites on d1.Sites.SiteId equals w.SiteId select d1; break; case "Department3":
Hi, If they are associated and mapped in the Entity Container, then you can simple load the reference's for your entity set. i.e. var b = from d1 in ents.Department1 select d1; List _departmentlist = b.tolist(); //now you can loop through this and load the sitereference for each department if you like like foreach(Department yourDept in _departmentlist) { yourDept.sitereference.load(); } Alternatively, have you thought about creating a view? A view is imported into your entity container as an entity set on its own, providing nice flexibility for complex situations.