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;
                    }
  • 相关阅读:
    C# Tostring()方法
    sql order by和case THEN 并用
    Lazarus Reading XML- with TXMLDocument and TXPathVariable
    Lazarus Reading XML- with TXMLDocument and TDOMNode
    Lazarus Coolbar and AnchroDocking
    Windows live writer 2012 测试
    组态王数据字典区块定义
    组态软件状态指示定义
    西门子Step7中DB块结构导出
    Delphi 不用标题栏移动窗体
  • 原文地址:https://www.cnblogs.com/zhanjindong/p/2804701.html
Copyright © 2011-2022 走看看