2013-02-14 28 views
0

我想一個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 
id 
patient_id 
plan_id 
site_id 

我想拉回來所有這一切都沒有被分配到一個計劃或排除患者計劃。

什麼決定了患者是否未被分配到計劃,是他們是否在排除表中,他們在plan_schedule表中沒有計劃並且他們不存在於plan_patient表中。

這在存儲過程中很容易做到,但我試圖將其構建出來,這樣我就不需要執行存儲過程來撤回結果。

+1

你爲什麼分開你的plan_patient,plan_exclusion和plan_schedule列到新表中?所有其他表格列是相同的。 – 2013-02-14 18:27:02

+0

這將有助於瞭解類的樣子,特別是導航屬性。 – 2013-02-14 22:05:54

回答

0

這是我已經接觸了多個表的複雜

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