C# 跨多个表使用实体框架
我试图创建一个linq语句,其中有以下四个表C# 跨多个表使用实体框架,c#,sql-server,entity-framework-5,C#,Sql Server,Entity Framework 5,我试图创建一个linq语句,其中有以下四个表 table: plan id planname table: patient Fields id, firstname, lastname, site_id Table: site id, sitename table: plan_patient id site_id patient_id table: plan_Exclusions id patient_id plan_id site_id table: plan_schedule i
table: plan
id
planname
table: patient
Fields
id, firstname, lastname, site_id
Table: site
id,
sitename
table: plan_patient
id
site_id
patient_id
table: plan_Exclusions
id
patient_id
plan_id
site_id
table: plan_schedule
id
patient_id
plan_id
site_id
我想收回所有未被分配到计划或未被排除在计划之外的患者
确定患者是否未分配到计划的是,他们在排除表中,他们在plan\u schedule
表中没有时间表,并且他们在plan\u patient
表中不存在
这在存储过程中是很容易做到的,但我正在尝试构建它,这样我就不需要执行存储过程来提取结果。这就是我如何为一个复杂的应用程序访问多个表的方式
var MyResults =
from hc in context.hcTypes
from hga in context.hgaToGmuTypes
from hq in context.hqToQuota
from qt in context.Types
from dd in context.ddDraws
from dh in context.dhDraws
where hc.Year == dtYear
&& hc.Year == hga.Year
&& hc.code == hga.code
&& hc.Year == hq.Year
&& hc.code == hq.code
&& hq.Id == qt.Id
&& qt.PrefernceCode == "Y"
&& hga.Year == dtYear
&& hga.Code == "Z"
&& hc.code == dd.code
&& dd.Code == dh.Code
&& dh.Year == dtYear
&& dh.Code == "Z"
&& dh.Left == "P"
select new MyClass { Id = hc.Id, Huntcode = hc.Huntcode, GMU = hga.GMUTypeCode }
;
在您的情况下,它将类似于:
var YourResults =
from pl in plan
from pa in patient
from s = site
from plan_patient
from plan_Exclusions
with the Where statements linking the data
and the Select pulling what you want
为什么要将“计划-患者”、“计划-排除”和“计划-时间表”列分离到新表中?所有其他表列都是相同的。了解类的外观,尤其是导航属性,会有所帮助。