linq to sql“into”条件与select

时间:2012-11-12 23:01:16

标签: c# sql linq

我有一个Invoice类,我希望有多个包,但我只希望这些包在它们出现的时候比Record中的最后一个记录晚。我一直在使用"进入"让其他群体进入对象,但我只是努力加入特定的包。

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)

此查询可以使用,但如果有多个包,我会得到多个相同的发票。如果我将"添加到包裹中#34;在加入结束时,我不知道如何只获得满足第二个条件的包。

子查询是:

(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

2 个答案:

答案 0 :(得分:0)

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

This would also solve your problem

答案 1 :(得分: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)}

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