EF Core how to query for a specific condition in related entities and update parent entity

raysefo

Well-known member
Joined
Feb 22, 2019
Messages
362
Programming Experience
10+
Hello,

In my Blazor Server application, I have Order and Order Details entities which are related as you can imagine. They both have their own repositories.

C#:
public class Order


    {


        public int Id { get; set; }       


        [Required]


        public DateTime OrderDateTime { get; set; }


        [Required]


        [MaxLength(250)]


        public int CustomerId { get; set; }


        public string Status { get; set; }


        [MaxLength(50)]


        public string DoneBy { get; set; }


        public List<OrderDetail> OrderDetails { get; set; }


        public Customer Customer { get; set; }


    }

C#:
public class OrderDetail


    {


        public int Id { get; set; }       


        [Required]


        [MaxLength(100)]


        public string ProductCode { get; set; }


        


        [MaxLength(250)]


        public string? ProductName { get; set; }


        [Required]


        public int Quantity { get; set; }


        [Required]


        public double BuyUnitPrice { get; set; }


        public double CostRatio { get; set; }


        public double UnitCost { get; set; }


        public double TotalBuyPrice { get; set; }


        public double? SellUnitPrice { get; set; }


        public double? TotalSellPrice { get; set; }


        [MaxLength(150)]


        public string? ShippingNumber { get; set; }


        public string? Status { get; set; }


        [MaxLength(150)]


        public string? TrackingNumber { get; set; }


        [MaxLength(400)]


        public string? Description { get; set; }


        public string? Currency { get; set; }


        public string? CustomerStockCode { get; set; }


        public string? CustomerOrderNumber { get; set; }


        public int IsActive { get; set; }


        public double? TotalUnitCost { get; set; }


        public int OrderId { get; set; }


        public int VendorId { get; set; }


        public string? Warehouse { get; set; }


        public string? PaymentStatus { get; set; }


        public Order Order { get; set; }


        public Vendor Vendor { get; set; }


        [DataType(DataType.DateTime)]


        public DateTime? CompletionDateTime { get; set; }


    }

I want to update the Order entity status = Completed, only if all of the related Order Detail statuses are Completed. Let's say, order id = 1, and if all of the Order Details statuses = Completed which are associated with Order = 1.

How can I achieve this?

Thank you.
 
Did you try using the LINQ All() extension method to check all the of the Order.OrderDetails ?
 
Solution
And then unset it if any one of the statuses becomes unset?

have you tried a construct like one of:

C#:
  var o = context.Orders.FirstOrDefault(o => o.OrderId == 1 && o.OrderDetails.All(od => od.Status == "Completed"));

  var o = context.Orders.FirstOrDefault(o => o.OrderId == 1 && !o.OrderDetails.Any(od => od.Status != "Completed"));

I'd expect it to translate to something like SELECT * FROM Orders o WHERE ID = 1 AND NOT EXISTS(SELECT null FROM OrderDetails od WHERE o.ID = od.ID AND od.Status <> "Completed"), perhaps with some null trickery in there too
 

Latest posts

Back
Top Bottom