如何使用lambda表达式使用EXIST和NOT EXIST子句

时间:2017-09-06 20:49:48

标签: c# entity-framework linq linq-to-entities

我正在尝试过滤掉没有交付事件类型的订单,并且工作正常。

List<Order> matchingOrders = xyzContext.Orders.Where(
                    o => o.FacilityId == facility.MasterFacilityId
                   && rxNumberList.Contains(o.RxNumber)
                   && o.OrderEvents.All(oe => oe.EventType.Code != EventTypeCode.Delivered)).ToList();

之后我试图过滤具有事件类型的订单&#34; Ordered&#34;并且没有事件类型&#34;已交付&#34;而且我没有得到预期的结果。

List<Order> matchingOrders = xyzContext.Orders.Where(
                    o => o.FacilityId == facility.MasterFacilityId
                   && rxNumberList.Contains(o.RxNumber)
                   && o.OrderEvents.All(oe => oe.EventType.Code != EventTypeCode.Delivered && oe.EventType.Code == EventTypeCode.Ordered)).ToList();

1 个答案:

答案 0 :(得分:0)

试试这个:

List<Order> matchingOrders = xyzContext.Orders.Where(
                o => o.FacilityId == facility.MasterFacilityId
               && rxNumberList.Contains(o.RxNumber)
               && o.OrderEvents.All(oe => oe.EventType.Code != EventTypeCode.Delivered)
               &&  o.OrderEvents.Any(oe.EventType.Code == EventTypeCode.Ordered)).ToList();