2012-11-12 72 views
2

我有一個Invoice類,我想要多個包,但我只希望這些包在它們的發貨時間晚於Record中的最後一個記錄。我一直在使用「進入」來將其他組放入對象中,但我只能加入特定的包。linq to sql「into」with select

from invoice in invoices 
join item in InvoiceItems on invoice.InvoiceId equals item.InvoiceId into items // Gives the item collection 
join package in packages on invoice.InvoiceId equals package.InvoiceId // only want the packages that satisfy the condition... 
where package.ShipDate > (subquery) 

上面的查詢工作,但如果有多個包我會得到相同的發票多。如果我在連接的末尾添加「包」,我不知道如何獲得滿足第二個條件的包。

子查詢:

(from lastSentRecord in records 
where lastSentRecord.InvoiceID == invoice.InvoiceID 
orderby lastSentRecord.SendDateTime descending 
select lastSentRecord.SendDateTime ?? new DateTime()).First() 

類結構的重要組成部分,是

Invoice --- InvoiceID 
Package --- InvoiceID, ShipDate 
Record --- InvoiceID, SendDateTime, EmailType 
+0

「Record」進來了嗎?特別是,這是否取決於「發票」? –

+0

你能提供你的數據集的結構嗎? – m4ngl3r

+0

@JonSkeet該記錄是通知我們需要發送電子郵件。它有InvoiceID,EmailType,SentDate。我會將子查詢添加到問題 –

回答

0

作爲工作的時候,我做了以下內容:

from invoice in invoices 
join packge in packages on invoice.InvoiceId equals package.InvoiceId into packages 
where (from package in packages 
     where package.InvoiceId == invoice.InvoiceId 
     where package.ShipDate > timeSent 
     select 1).Any() 
select new {packages.Where(p => p.ShipDate > timeSent)} 

它能夠完成任務,但我希望做一個更好的辦法。我會再打開一段時間,看看有沒有人有答案,然後我會接受這個答案。

0

你可以改變你讓他們兩個單獨的查詢,然後滿足第二個條件(可能撥打第二首先讓你可以爲where創建變量)。以下文章向您展示瞭如何在select語句中執行此操作。

This would also solve your problem

+0

我知道如何使用單獨的查詢。我不知道他們將如何適用於這種情況。 –