2

我有一个 Invoice 类,我希望有多个包,但我只希望这些包在它们的发货晚于记录中的最后一条记录时才存在。我一直在使用“into”来让其他组进入对象,但我正在努力只加入特定的包。

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
4

2 回答 2

0

您可以将它们更改为两个单独的查询,然后满足第二个条件(可能首先调用第二个条件,以便您可以为 where 创建变量)。下面的文章向您展示了如何通过在 select 语句中执行此操作来做到这一点。

这也可以解决您的问题

于 2012-11-15T09:28:20.170 回答
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)}

它完成了工作,但我想以更好的方式完成它。我将把这个打开一段时间,看看是否有人有答案,然后我会接受这个作为答案。

于 2012-11-16T19:45:22.503 回答