DbContext Check all values and compare

Cenk 956 Reputation points
2022-08-10T12:29:01.09+00:00

Hello,

I have orders and order details. I want to check all the order details in order to find out if the status of all the order details is completed. There are a couple of statuses; canceled, completed, continues. How to check the statuses in the sample below in order to know if all order details are completed?

await this._db.OrderDetails.Where(x => x.OrderId) == id);  

Thanks.

Entity Framework Core
Entity Framework Core
A lightweight, extensible, open-source, and cross-platform version of the Entity Framework data access technology.
697 questions
0 comments No comments
{count} votes

2 answers

Sort by: Most helpful
  1. AgaveJoe 26,191 Reputation points
    2022-08-10T18:55:06.543+00:00

    Keep in mind, we cannot see the schema. If OrderDetails has a Status column then you can get the status value by OrderId using the following.

     (await this._db.OrderDetails.FirstOrDefault(x => x.OrderId == id)).Status;  
    

    If you want to filter by the status column and the status column is a string type then the LINQ syntax is...

    this._db.OrderDetails.Where(x => x.Status == "completed");  
    

    The C# programming guide and EF Core fundamentals cover LINQ query syntax.

    Introduction to LINQ Queries (C#)
    Querying Data


  2. Karen Payne MVP 35,036 Reputation points
    2022-08-16T12:33:04.307+00:00

    I would recommend storing the status as an int and relate to a status table e.g.

    231518-dvhrms.png

    The code to get statues using an order identifier

    public class DataOperations  
    {  
      
        public static List<OrderDetails> OrdersDetailsList(int orderId, int[] identifiers)  
        {  
            using var context = new NorthContext();  
            return context  
                .OrderDetails  
                .Include(x => x.Product)  
                .Include(x => x.Status)  
                .Where(x => x.OrderId == orderId && identifiers.Contains(x.Status.StatusId))  
                .ToList();  
        }  
    }  
    

    If you are using SQL-Server, see full source which shows the above method and the above method with a single status.

    0 comments No comments