2010-03-22 179 views
3

能否請你幫我弄清楚這一個? 我需要更換一個連接與OSLP表外連接。似乎有人誰不是在LINQ到實體專家有點棘手。我會怎麼做?LINQ到實體 - LEFT OUTER JOIN

var surgeonList = (
    from item in context.T1_STM_Surgeon 
     .Include("T1_STM_SurgeonTitle") 
     .Include("OTER") 
    where item.ID == surgeonId 
    join reptable in context.OSLP 
     on item.Rep equals reptable.SlpCode 
    select new 
    { 
     ID = item.ID, 
     First = item.First, 
     Last = item.Last, 
     Rep = reptable.SlpName, 
     Reg = item.OTER.descript, 
     PrimClinic = item.T1_STM_ClinicalCenter.Name, 
     Titles = item.T1_STM_SurgeonTitle, 
     Phone = item.Phone, 
     Email = item.Email, 
     Address1 = item.Address1, 
     Address2 = item.Address2, 
     City = item.City, 
     State = item.State, 
     Zip = item.Zip, 
     Comments = item.Comments, 
     Active = item.Active, 
     DateEntered = item.DateEntered 
    }).ToList(); 

在此先感謝!

回答

0

語法如下,讓我知道,如果你有麻煩的轉換代碼。 通用外連接(組加入):

VAR的查詢= 從l在左

join r in right 
    on l.ID 
    equals l.right.ID into groupedJoin 
    select new 
    { 
     ID= l.ID, 
     OuterJoined= groupedJoin.Select(r=> right) 
    }; 

你的結果是,即使正確不存在失去了一切。

很明顯,我不能保證它會編譯,但它應該是這樣的:

VAR surgeonList =(

from item in context.T1_STM_Surgeon 
     .Include("T1_STM_SurgeonTitle") 
     .Include("OTER") 
    where item.ID == surgeonId 
    join reptable in context.OSLP 
     on item.Rep equals reptable.SlpCode into groupedJoin 
    select new 
    { 
     ID = item.ID, 
     First = item.First, 
     Last = item.Last, 
     Rep = reptable.SlpName, 
     Reg = item.OTER.descript, 
     PrimClinic = item.T1_STM_ClinicalCenter.Name, 
     Titles = item.T1_STM_SurgeonTitle, 
     Phone = item.Phone, 
     Email = item.Email, 
     Address1 = item.Address1, 
     Address2 = item.Address2, 
     City = item.City, 
     State = item.State, 
     Zip = item.Zip, 
     Comments = item.Comments, 
     Active = item.Active, 
     DateEntered = item.DateEntered 
     OSLP = groupedJoin.Select(x=>WHATEVERYOUNEED) 
}).ToList(); 

而你只需要更換groupedJoin.Select(X => WHATEVERYOUNEED)無論使用哪種選擇的東西或一個簡單的.ToList

+0

你能編輯您的文章,包括代碼塊,好嗎?這是不可能的閱讀。 – 2010-03-30 15:12:45

+0

我做後不久,我看到了混亂...是更好? – Nix 2010-03-30 17:50:54