2012-11-12 71 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

我知道如何使用单独的查询。我不知道他们将如何适用于这种情况。 –