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;
                    }
  • 相关阅读:
    父子项目
    生成资源文件时候,可以动态替换为maven属性
    生命周期阶段与插件目标任务绑定
    私服
    仓库
    DotNetBar教程
    SQL基础--> 约束(CONSTRAINT)
    jQuery.Autocomplete实现自动完成功能(详解)
    C# 解析JSON格式数据
    JSON 数据格式解析
  • 原文地址:https://www.cnblogs.com/zhanjindong/p/2804701.html
Copyright © 2011-2022 走看看