【问题标题】:How can I query the range of date which one product has not been ordered? (LINQ)如何查询未订购某一产品的日期范围? (LINQ)
【发布时间】:2009-04-20 09:09:58
【问题描述】:

我有三张桌子,

  • 产品(ProductID、ProductName)
  • 订单(OrderID、OrderDate)
  • OrderDetails(ProductID, OrderID) Products 和 Orders 之间的连接表。

例如,有 Products表中的三个ProductID A01、A02、A03

Orders 表中有四个 OrderID 1. 01/01/2009, 2. 03/01/2009, 3. 05/01/2009, 4. 07/01/2009.

订单详情数据 如下:

A01 -- 01/01/2009

A02 -- 01/01/2009

A01 -- 03/01/2009

A02 -- 03/01/2009

A01 -- 05/01/2009

A03 -- 05/01/2009

A01 -- 07/01/2009

然后我想显示从 02/01/2009 到 08/01/2009 范围内未订购 A03 的日期,这可能会产生这样的结果。

DateMissingOrder 产品 A03 的日期范围为 02/01/2009 至 08/01/2009:

03/01/2009

2009 年 7 月 1 日

如何在 LINQ to SQL Visual Basic 中执行此操作?请有人帮助我提供一些线索..提前致谢。

【问题讨论】:

    标签: vb.net linq linq-to-sql


    【解决方案1】:

    试试这样的:

    选择范围内的所有日期

    Dim DatesInRange = (From o in dc.Orders _
                       Where (o.OrderDate >= StartDate and _
                              o.OrderDate <= EndDate)
                       Select o.OrderDate).Distinct
    

    选择A03订单范围内的所有日期

    Dim Dates = (From o in dc.OrdersDetails _
                Where (o.Order.OrderDate >= StartDate and _
                       o.Order.OrderDate <= EndDate) and _
                       o.Order.Product.ProductName = "A03" _
                Select o.Order.OrderDate).Distinct
    

    返回第二个列表中缺少的日期

    Dim MissingDates = DatesInRange.Except(Dates)
    

    【讨论】:

    • 非常感谢您的回答.. 但我们正在寻找产品名称 A03 的日期缺失日期。在您的情况下,您只选择不是 A03 的产品。
    • 非常感谢!!!!!!!!!!!!!!!现在问题解决了!!你是世界上最聪明的人!!!!谢谢
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2015-05-12
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多