武装你的WEBAPI-OData聚合查询

Introduction

 

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>
        [Key, DatabaseGenerated(DatabaseGeneratedOption.Identity)]
        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>
        [Key, DatabaseGenerated(DatabaseGeneratedOption.Identity)]
        public int Id { get; set; }
        [Required]
        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>
        [Key]
        [MaxLength(200)]
        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))
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 打赏
    打赏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

icwx_7550592

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值