2012-02-21 71 views
0

我想要做一個如果聲明在我的查詢內,但我不知道如何。我想執行:如何通過條件加入?

join asoc2 in Personnels 
on comp.Secondary_associate equals asoc2.Dpinitials 

如果有Second_Person數據可用。任何想法如何做到這一點?讓我知道是否有更簡單的方法。這是我的完整查詢:

(from sc in Data 
join comp in Companies 
on sc.Company equals comp.Company 
join anl in Personnels 
on sc.SalesP_initials equals anl.Dpinitials 
join asoc1 in Personnels 
on comp.First_associate equals asoc1.Dpinitials 
join asoc2 in Personnels 
on comp.Second_Person equals asoc2.Dpinitials 
select new { 
MyCompany = sc.Company, 
ReleaseDate = sc.Release_date, 
WebHeadline = sc.Short_subject, 
EmailTo = 
    anl.Last_name + ", " + anl.First_name + " <" + anl.Email_address.Trim() + ">; " 
    + 
    asoc1.Last_name + ", " + asoc1.First_name + " <" + asoc1.Email_address.Trim() + ">; " 
    + 
    asoc2.Last_name + ", " + asoc2.First_name + " <" + asoc2.Email_address.Trim() + ">" 
}).FirstOrDefault() 

現在,它只是顯示null,因爲* comp.Second_Person *不存在。

+0

你嘗試加入'那裏comp.Second_Person!= null'畢竟加入之前選擇新的? – MarcinJuraszek 2012-02-21 21:21:41

+2

當您避開查詢理解語法並改爲使用方法鏈式樣時,根據運行時值有條件地組合查詢的各個部分和部分將變得微不足道。 – 2012-02-21 21:23:33

+0

@KirkWoll你會提供一些細節嗎? – 2012-02-22 03:46:05

回答

0

您可以使用「其中」在LINQ過濾數據