【问题标题】:Get me the other items also bought with particular product, in LINQ?给我在 LINQ 中用特定产品购买的其他物品吗?
【发布时间】:2017-10-30 18:13:57
【问题描述】:

我有以下两张表:

  • 订单:ID、其他信息
  • 产品:ID、名称、其他信息
  • OrderProduct:OrderId、ProductId、其他信息

我们都知道,两个订单可以包含多个产品,而这两个订单之间可以有一个或多个共同的产品。

什么是 LINQ 用于拉取与特定产品一起购买的其他不同产品?有一个简单的 SQL 答案,但我需要 LINQ 等价物。

【问题讨论】:

    标签: sql linq


    【解决方案1】:
    var matchingOrders = from op in OrderProduct
                         where op.ProductId == desiredProductId
                         select OrderId;
    var otherProductsOnSameOrder = (from op in OrderProduct
                                    where matchingOrders.Contains(op.OrderId) && op.ProductId != desiredProductId
                                    select op.ProductId).Distinct();
    

    【讨论】:

      猜你喜欢
      • 2016-10-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2019-08-16
      • 2019-01-27
      • 1970-01-01
      相关资源
      最近更新 更多