LINQ to Entities - 在我的示例中排除部分发货订单的 WHERE 子句
我有一个订单表。一个订单中可以包含多个商品,每个商品在不同的时间发货。我想获取所有订单的列表,不包括部分发货或订单。换句话说,我需要获取所有已完全发货的订单的列表。我可能知道如何在 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 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
假设您的数据有假列表:
那么您的 linq 查询应该如下所示:
这样您就会得到几个字符串 -
"00001" 和 "00003"
作为结果。因此,对于真正的数据库查询,您可以编写如下内容:
Let's suppose that you have fake list with your data:
Then your linq query should look like this:
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: