加入所有子列表并抓住第一个

时间:2013-01-22 16:08:43

标签: c# .net vb.net linq linq-to-objects

我希望能够提取包含类别中产品的最后一张发票 类别 - >制品 - >发票

(from p as Product in cat
 where p.InvoiceList.Where(function(o) o.InvoiceDate >= MAX_ONE)
 select p.InvoiceList.Where(function(o) o.InvoiceDate >= MAX_ONE)
).FirstOrDefault()

我似乎无法理解如何完成这项工作。

编辑:一个可以实现我的目标的示例SQL语句。如果我能翻译它......

SELECT    TOP 1 i.InvoiceID, i.InvoiceDate, i.TotalAmount
FROM         Category as c INNER JOIN
                  Product as p ON p.categoryID = c.categoryID INNER JOIN
                  InvoiceProducts as ip ON ip.productID = p.productID INNER JOIN
                  Invoice as i ON ip.InvoiceID = i.InvoiceID 
WHERE     c.categoryID = 3
ORDER BY InvoiceDate DESC

2 个答案:

答案 0 :(得分:1)

cat.SelectMany(p => p.InvoiceList).OrderBy(o => o.InvoiceDate).LastOrDefault();

答案 1 :(得分:0)

var lastInvoice = (from i in context.Invoices
    .where i.Product.CategoryId == categoryId
    .select i)
        .OrderByDescending(i=>i.IbvoiceDate)
        .FirstOrDefault();