【问题标题】:Struggling to convert SQL to LINQ努力将 SQL 转换为 LINQ
【发布时间】:2012-06-30 17:02:53
【问题描述】:

我有一个 Linq 表达式(对于北风)获取客户 id 为“ALFKI”的每个产品订购的总数量

from od in db.OrderDetails
    where od.Order.CustomerID == "ALFKI"
    group od by od.Product.ProductName into g1
    select new { ProductName = g1.Key, Total = g1.Sum(od => od.Quantity) }

这很好,但要完全理解 Linq,我想尝试在 Linq2Sql 不能很好地通过外键构建属性桥的世界中制定表达式。

例如,在上面的表达式中,我正在访问 od.Order.CustomerID。我想假设 od.OrderID 已尽我所能。

查看表达式的 SQL,我有:

SELECT SUM(CONVERT(Int,[t0].[Quantity])) AS [Total], [t2].[ProductName]
FROM [Order Details] AS [t0]
INNER JOIN [Orders] AS [t1] ON [t1].[OrderID] = [t0].[OrderID]
INNER JOIN [Products] AS [t2] ON [t2].[ProductID] = [t0].[ProductID]
WHERE [t1].[CustomerID] = @p0
GROUP BY [t2].[ProductName] 

据我所知:

from od in db.OrderDetails
    join o in db.Orders on od.OrderID equals o.OrderID
    join p in db.Products on od.ProductID equals p.ProductID
    where o.CustomerID == "ALFKI"
    group od by od.ProductID into g1
    select new { ProductName = g1.Key, Total = g1.Sum(od => od.Quantity) }

这几乎就在那里,但 g1.Key 指的是 ProductID。我似乎无法同时获得 ProductName 和订单数量。

谢谢

【问题讨论】:

    标签: c# .net sql linq linq-to-sql


    【解决方案1】:

    尝试通过以下方式为您的群组创建一个新的匿名类型:

    from od in db.OrderDetails
        join o in db.Orders on od.OrderID equals o.OrderID
        join p in db.Products on od.ProductID equals p.ProductID
    where o.CustomerID == "ALFKI"
    group od by new { p.ProductID, p.ProductName } into g1
    select new { 
        ProductName = g1.Key.ProductName, 
        Total = g1.Sum(od => od.Quantity) }
    

    【讨论】:

    • 谢谢,这很好,虽然需要稍作修正。它应该是 { od.ProductID, p.ProductName }。
    • 啊,好吧...我已经更新了答案。我相信它应该适用于{ p.ProductID, p.ProductName }。是吗?
    • 我知道复合键,但我没有想到要引用其他实体的字段。还有一个问题。是否有可能通过一个新变量引用整个连接(od、o 和 p)然后迭代它? IE。 myJoin = (od, o, p);来自 MyJoin 中的项目 ...
    • 我之前忘了说它确实有效。在发表评论之前,我已经进行了测试。
    • 嗯...我不确定我是否理解您的问题。也许您可以将其作为一个带有完整示例的新问题发布。这将使问题更容易理解。或者只是试一试,看看它是否适合你。
    【解决方案2】:

    除了凯文的回答,使用复合键,我还设法做到了另一种方式:

    from item in (from od in db.OrderDetails
                      join o in db.Orders on od.OrderID equals o.OrderID
                      join p in db.Products on od.ProductID equals p.ProductID
                      where o.CustomerID == "ALFKI"
                      select new {ProductName = p.ProductName, Quantity = od.Quantity }
                  )
                  group item by item.ProductName into g1
                  select new {ProductName = g1.Key, Total = g1.Sum(od=>od.Quantity) }
    

    更一般地说,如果您想将所有连接拼接在一起并引用任何字段:

    from item in (from od in db.OrderDetails
                     join o in db.Orders on od.OrderID equals o.OrderID
                     join p in db.Products on od.ProductID equals p.ProductID
                     where o.CustomerID == "ALFKI"
                     select new { p, od }
                 )
                 group item by item.p.ProductName into g1
                 select new { ProductName = g1.Key, Total = g1.Sum(all => all.od.Quantity)}
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2014-03-10
      • 1970-01-01
      • 1970-01-01
      • 2023-02-05
      • 1970-01-01
      • 2022-12-05
      • 2019-05-21
      • 1970-01-01
      相关资源
      最近更新 更多