OData Property count of entire collection

旧城冷巷雨未停 提交于 2021-01-29 02:27:56

问题


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.

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"
        }
      }
    ]
  }
  

来源:https://stackoverflow.com/questions/64392114/odata-property-count-of-entire-collection

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!