WebApi icon indicating copy to clipboard operation
WebApi copied to clipboard

Return count of a property as it appears in the parent list of records

Open adixon501 opened this issue 4 years ago • 3 comments

Not an issue as much as it is a question, but I need to know if something is possible or if I need to go a different route with my need. I made a SO question for it where you can find the details. Sorry if this is unconventional, but I'm in a holding pattern until I find the answer. Thanks in advance

Assemblies affected

Microsoft.AspNetCore.OData v7.5.0

link to stack overflow post

adixon501 avatar Oct 21 '20 15:10 adixon501

Hi I believe you are trying to get the count of orders for a customer , the other way works right? cna you share how your model is set up

Sreejithpin avatar Nov 02 '20 22:11 Sreejithpin

@Sreejithpin

I have an asp.net core 3.2 api which uses ef core and OData to return data to the client.

I need to be able return the count of an expanded one-to-one property in a list of objects along with the parent object list. Since I am paging the results, the @odata.count represents the count of records in the database that match the criteria, not the count of records it actually returns.

To explain lets say we have a couple of classes

    public class Order
    {
        public int Id { get; set; }
        public string Name { get; set; }
        public Customer Customer { get; set; }
    }

    public class Customer
    {
        public int Id { get; set; }
        public string Name { get; set; }
        public ICollection<Order> Orders { get; set; }
    }

Now I've seen other Stackoverflow Q&A that will return the number of Orders within a Customer.

But I need to return a list of Orders and while expanding the Customer, I need to know how many times a Customer appears in the list of Orders.

To provide some context as to why I need this, is because I am building a list filter form like this below. Users can then click the checkbox to filter the list by Customer.

Filter criteria

Using this query for starters:

https://localhost:5001/odata/Orders?$count=true&$expand=Customer&$filter=Some criteria&$top=7&$skip=0

So a desired result would be:

{
    "@odata.context": "https://localhost:5001/odata/$metadata#Orders(Customer())",
    "@odata.count": 20, // the count of Orders that match criteria in database
    "value": [
      {
        "Id": 1,
        "Name": "Order 1",
        "Customer": {
            "@odata.count": 6, // this is what i need the count of Customers that are in Orders that match criteria in database
            "Id": 1,
            "Name": "Contoso"
        }
      },
      {
        "Id": 2,
        "Name": "Order 2",
        "Customer": {
            "@odata.count": 6,
            "Id": 1,
            "Name": "Contoso"
        }
      },
      {
        "Id": 3,
        "Name": "Order 3",
        "Customer": {
            "@odata.count": 6,
            "Id": 1,
            "Name": "Contoso"
        }
      },
      {
        "Id": 4,
        "Name": "Order 4",
        "Customer": {
            "@odata.count": 4,
            "Id": 2,
            "Name": "Acme"
        }
      },
      {
        "Id": 5,
        "Name": "Order 5",
        "Customer": {
            "@odata.count": 4,
            "Id": 2,
            "Name": "Acme"
        }
      },
      {
        "Id": 6,
        "Name": "Order 6",
        "Customer": {
            "@odata.count": 10,
            "Id": 3,
            "Name": "Foo Bar Inc"
        }
      },
      {
        "Id": 7,
        "Name": "Order 7",
        "Customer": {
            "@odata.count": 10,
            "Id": 3,
            "Name": "Foo Bar Inc"
        }
      }
    ]
  }
  

adixon501 avatar Nov 02 '20 22:11 adixon501

@adixon501 The query below should work https://localhost:5001/odata/Orders?$count=true&$expand=Customer($count=true)&$filter=Some criteria&$top=7&$skip=0

Note: This should work when Customer is a collection

e.g

public class Order
{
    public int Id { get; set; }
    public string Name { get; set; }
    public List<Customer> Customers { get; set; }
}

KenitoInc avatar May 18 '22 11:05 KenitoInc

Closing this issue due to inactivity. If this issue still persists, feel free to create a new issue.

KenitoInc avatar Feb 10 '23 08:02 KenitoInc