zoukankan      html  css  js  c++  java
  • Linq GroupBy

    public class InterfaceKey : IComparable
        {
            /// <summary>
            /// 接口名称
            /// </summary>
            public string InterfaceName = string.Empty;
            /// <summary>
            /// 产品标识
            /// </summary>
            public string Aid = string.Empty;
            /// <summary>
            /// 版本标识
            /// </summary>
            public string Version = string.Empty;
            /// <summary>
            /// 平台标识
            /// </summary>
            public string OSID = string.Empty;
            /// <summary>
            /// 表示时间的字符串(具体到分钟。如:201206010001)
            /// </summary>
            public string FileName;
    
            public int CompareTo(object obj)
            {
                if (obj.GetType() != this.GetType())
                {
                    return -1;
                }
                InterfaceKey selfObj = obj as InterfaceKey;
                int result = 0;
                if (this.InterfaceName == selfObj.InterfaceName && this.Aid == selfObj.Aid && this.Version == selfObj.Version && this.OSID == selfObj.OSID)
                {
                    result = 1;
                }
                else
                    result = -1;
                return result;
            }
            public override bool Equals(object obj)
            {
                if (obj.GetType() != this.GetType())
                {
                    return false;
                }
                InterfaceKey other = obj as InterfaceKey;
                if (this.InterfaceName != other.InterfaceName || this.Version != other.Version || this.Aid != other.Aid || this.OSID != other.OSID)
                {
                    return false;
                }
                return true;
            }
    
            public override int GetHashCode()
            {
                return this.InterfaceName.ToString().GetHashCode();
            }
    
        }
    /// <summary>
        /// 封装接口需要统计的信息的数据结构(可扩展)
        /// </summary>
        public class InterfaceInfo
        {
            /// <summary>
            /// 接口调用次数
            /// </summary>
            public Int64 Frequency;
    
            /// <summary>
            /// 接口调用耗时
            /// </summary>
            public Int64 TimeConsuming;
        }
    //Linq GroupBy hourMergerInfo:Dictionary<InterfaceKey, InterfaceInfo>
    var query = hourMergerInfo.GroupBy(v => v.Key.InterfaceName).ToDictionary(x => x.Key,
                    x => new
                    {
                        SumFrequency = x.Sum(y => y.Value.Frequency),
                        AvgTimeConsuming = x.Average(y => y.Value.TimeConsuming * 1.0 / y.Value.Frequency)
                    });
    
     foreach (var item in query)
                    {
                        string key = item.Key;
                        Int64 sumFrequency = item.Value.SumFrequency;
                        double avgTimeConsuming = item.Value.AvgTimeConsuming;
                    }
  • 相关阅读:
    sublime & atom 插件
    正则表达式必知必会读书笔记
    前端自动化工具
    CSS3效果收集
    JS 常用功能收集
    【1】Hover 效果收集
    javascript进阶高手必备知识
    ionic 实现仿苹果手机通讯录搜索功能
    ionic2APP 如何处理返回键问题
    三张图搞懂JavaScript的原型对象与原型链
  • 原文地址:https://www.cnblogs.com/zhanjindong/p/2804701.html
Copyright © 2011-2022 走看看