LINQ to Entities - 在我的示例中排除部分发货订单的 WHERE 子句

发布于 2024-12-27 15:46:38 字数 1198 浏览 2 评论 0原文

我有一个订单表。一个订单中可以包含多个商品,每个商品在不同的时间发货。我想获取所有订单的列表,不包括部分发货或订单。换句话说,我需要获取所有已完全发货的订单的列表。我可能知道如何在 T-SQL 中执行此操作。但我正在尝试使用 LINQ-to-Entities (EF4/.Net 4.0/C#) 来完成此任务。

考虑以下数据:

PK      OrderID      Item       Status
1       00001        TV         Shipped
2       00001        TABLET     Shipped
3       00002        BLURAYPL   Not Shipped
4       00002        MOBILEPH   Shipped
5       00002        XBOX       Shipped
6       00003        PAPER      Shipped
7       00003        PENCIL     Shipped

目标是获得 00001 和 00003 作为输出。

这是我到目前为止所得到的,明显简化了:

using (MyDBEntities dbcontext = new MyDBEntities())
{
  var WashingtonClients = from a in dbcontext.CustomerMasterTable
                    where a.City == "Washington"
                    select a.CustomerID;

 var ShippedOrdersToWashingtonClients = from o in dbcontext.OrderDetail
                     where WashingtonClients.Contains(o.CustomerID)
                     && o.Status.ToUpper() == "SHIPPED"
                     //how to exclude Partially Shipped orders here???
                     select o.OrderID;
}

如何以排除包含单个未发货商品的订单的方式构建第二个查询?非常感谢您抽出时间。

I have an Order table. An Order can have multiple items in it, with each item shipping at different times. I want to get the list of all Orders which excludes partially shipped or orders. In other words, I need to get the list of all orders that are completely shipped. I may know how to do this in T-SQL. But I'm trying to accomplish this with LINQ-to-Entities (EF4/.Net 4.0/C#).

Consider the following Data:

PK      OrderID      Item       Status
1       00001        TV         Shipped
2       00001        TABLET     Shipped
3       00002        BLURAYPL   Not Shipped
4       00002        MOBILEPH   Shipped
5       00002        XBOX       Shipped
6       00003        PAPER      Shipped
7       00003        PENCIL     Shipped

The goal is to get 00001 and 00003 as output.

Here is what I have so far, obviously simplified :

using (MyDBEntities dbcontext = new MyDBEntities())
{
  var WashingtonClients = from a in dbcontext.CustomerMasterTable
                    where a.City == "Washington"
                    select a.CustomerID;

 var ShippedOrdersToWashingtonClients = from o in dbcontext.OrderDetail
                     where WashingtonClients.Contains(o.CustomerID)
                     && o.Status.ToUpper() == "SHIPPED"
                     //how to exclude Partially Shipped orders here???
                     select o.OrderID;
}

How to frame the second query in such a way that it excludes orders that have even a single unshipped item in it? Many thanks for your time.

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

孤芳又自赏 2025-01-03 15:46:38

假设您的数据有假列表:

var orderDetails = new List<OrderDetail>
    {
        new OrderDetail{ ID = 1, Item = "TV", OrderID = "00001", Status = "Shipped"},
        new OrderDetail{ ID = 2, Item = "TABLET", OrderID = "00001", Status = "Shipped"},
        new OrderDetail{ ID = 3, Item = "BLURAYPL", OrderID = "00002", Status = "NotShipped"},
        new OrderDetail{ ID = 4, Item = "MOBILEPH", OrderID = "00002", Status = "Shipped"},
        new OrderDetail{ ID = 5, Item = "XBOX", OrderID = "00002", Status = "Shipped"},
        new OrderDetail{ ID = 6, Item = "PAPER", OrderID = "00003", Status = "Shipped"},
        new OrderDetail{ ID = 7, Item = "PENCIL", OrderID = "00003", Status = "Shipped"}
    };

那么您的 linq 查询应该如下所示:

var result = orderDetails
    .GroupBy(o => o.OrderID)
    .Where(g => g.All(i => i.Status == "Shipped"))
    .Select(g => g.Key);

这样您就会得到几个字符串 - "00001" 和 "00003" 作为结果。

因此,对于真正的数据库查询,您可以编写如下内容:

dbContext.OrderDetails
    .Where(o => WashingtonClients.Contains(o.CustomerID))
    .GroupBy(o => o.OrderID)
    .Where(g => g.All(i => i.Status == "Shipped"))
    .Select(g => g.Key);

Let's suppose that you have fake list with your data:

var orderDetails = new List<OrderDetail>
    {
        new OrderDetail{ ID = 1, Item = "TV", OrderID = "00001", Status = "Shipped"},
        new OrderDetail{ ID = 2, Item = "TABLET", OrderID = "00001", Status = "Shipped"},
        new OrderDetail{ ID = 3, Item = "BLURAYPL", OrderID = "00002", Status = "NotShipped"},
        new OrderDetail{ ID = 4, Item = "MOBILEPH", OrderID = "00002", Status = "Shipped"},
        new OrderDetail{ ID = 5, Item = "XBOX", OrderID = "00002", Status = "Shipped"},
        new OrderDetail{ ID = 6, Item = "PAPER", OrderID = "00003", Status = "Shipped"},
        new OrderDetail{ ID = 7, Item = "PENCIL", OrderID = "00003", Status = "Shipped"}
    };

Then your linq query should look like this:

var result = orderDetails
    .GroupBy(o => o.OrderID)
    .Where(g => g.All(i => i.Status == "Shipped"))
    .Select(g => g.Key);

so that you get a couple of string - "00001" and "00003" as a result.

Hence for real query to db you can write something like this:

dbContext.OrderDetails
    .Where(o => WashingtonClients.Contains(o.CustomerID))
    .GroupBy(o => o.OrderID)
    .Where(g => g.All(i => i.Status == "Shipped"))
    .Select(g => g.Key);
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文