宇宙稊米 发表于 2023-3-27 12:44:09

武装你的WEBAPI-OData聚合查询

本文属于OData系列
目录


[*]武装你的WEBAPI-OData入门
[*]武装你的WEBAPI-OData便捷查询
[*]武装你的WEBAPI-OData分页查询
[*]武装你的WEBAPI-OData资源更新Delta
[*]武装你的WEBAPI-OData之EDM
[*]武装你的WEBAPI-OData常见问题
[*]武装你的WEBAPI-OData使用Endpoint
[*]武装你的WEBAPI-OData聚合查询
ODATA v4提出了新的聚合查询功能,这对于ODATA的基本查询能力($expand等)是一个非常大的补充。ODATA支持的聚合查询功能,可以对数据进行统计分析,例如求和、平均值、最大/最小值、分组等。
聚合查询是通过$apply关键字实现的。使用$apply关键字可以指定一系列的聚合操作,以对数据进行处理。
GET /odata/Products?$apply=groupby((Category), aggregate(Price with sum as TotalSales))该请求将返回按照产品类别(Category)分组的数据,并计算每个组的销售总额(TotalSales)。
需要注意,聚合查询出来的结果一般都不在EDM中注册,因此无法对结果应用更多ODATA查询,如果想解锁这个能力,请手动在EDM中注册这个类型。
聚合查询示例

ODATA的强大之处在于可以赋予前端更多的自主权,让他们自己获得自己需要的数据,通过聚合查询,我们可以实现非常多复杂的、以前只能在后端单独实现的查询。举例说明,我们有以下三个实体类。
public class AttachDeviceType
    {
      /// <summary>
      ///
      /// </summary>
      
      public int Id { get; set; }
      /// <summary>
      /// 附加设备的类型
      /// </summary>
      public string Name { get; set; }
      /// <summary>
      /// 描述
      /// </summary>
      public string Description { get; set; }
    }   public abstract class AttachDeviceInfo
    {
      /// <summary>
      ///
      /// </summary>
      
      public int Id { get; set; }
      
      public string AttachDeviceId { get; set; }
      public string Name { get; set; }
      /// <summary>
      /// 附加设备的类型
      /// </summary>
      public virtual AttachDeviceType AttachDeviceType { get; set; }
      public string? Description { get; set; }

      public virtual DeviceInfo DeviceInfo { get; set; }
    }   public class DeviceInfo
    {
      /// <summary>
      ///
      /// </summary>
      
      
      public string DeviceId { get; set; }
      /// <summary>
      /// 地域位置的代码
      /// </summary>
      public int? Adcode { get; set; }

      public virtual ICollection<AttachDeviceInfo> AttachDevices { get; set; }
    }以上三个类定义了主设备、附加设备与从属设备的类型,三者之间通过导航属性进行连接。假定我们需要按照地域位置代码进行分组查询,查询出每个地域的所有主设备的、附加设备的和不同的类型的数量。
/odata/DeviceInfos?$apply=groupby((Adcode), aggregate(AttachDevices/$count as NumAttachDevices, $count as NumDeviceInfos, AttachDevices/AttachDeviceType/$countdistinct as NumAttachDeviceTypes))或者我们使用以attachdevice作为目标
/odata/AttachDeviceInfo?$apply=groupby((DeviceInfo/Adcode), aggregate(
      $count as TotalDeviceInfo,
      attachdevices/$count as TotalAttachDevices,
      AttachDeviceType/$countdistinct as TotalAttachDeviceTypes))思路没有问题,但是实际执行会提示$count不是可用的aggregatebinder之类的错误(Binding OData QueryNode of kind 'Count' is not supported by 'AggregationBinder'.)。这是因为$count已经是OData进行查询得到的结果,这个结果不能在进行的聚合查询了。
换一个思路,我们使用每一个实体对象的属性作为统计对象。
odata/attachdeviceinfos?$apply=groupby((deviceinfo/Adcode), aggregate($count as NumAttachDevices, deviceinfo/deviceid with countdistinct as NumDevices, attachdevicetype/id with countdistinct as NumTypes))那么就可以得到正确的结果:
[

    {

        "deviceInfo": {

            "adcode": 110105

        },

        "NumTypes": 1,

        "NumDevices": 1,

        "NumAttachDevices": 2

    },

    {

        "deviceInfo": {

            "adcode": 110108

        },

        "NumTypes": 1,

        "NumDevices": 1,

        "NumAttachDevices": 1

    }
]当然,我们还可以组合使用filter查询:
/odata/attachdeviceinfos?$apply=filter(deviceinfo/Adcode eq 110105)/groupby((deviceinfo/Adcode), aggregate($count as NumAttachDevices, deviceinfo/deviceid with countdistinct as NumDevices, attachdevicetype/id with countdistinct as NumTypes))注意:在AXIOS中,前端发送如/之类的特殊字符会自动进行转移,导致查询请求失败,请前端搜索禁止AXIOS自动转移的方法,以保证发送的请求同POSTMAN发送的请求一致。
总结

使用聚合查询可以方便将原来需要后端单独编程的分类、统计等操作简化,给予了前端非常大的自由度,同时减少了后端的接口数量。需要注意的是,很多操作在聚合查询中语法可能有一些变化,请一定参阅ODATA的OASIS标准的文档。

来源:https://www.cnblogs.com/podolski/archive/2023/03/27/17261079.html
免责声明:由于采集信息均来自互联网,如果侵犯了您的权益,请联系我们【E-Mail:cb@itdo.tech】 我们会及时删除侵权内容,谢谢合作!
页: [1]
查看完整版本: 武装你的WEBAPI-OData聚合查询