zoukankan      html  css  js  c++  java
  • linq group by having 实现

    TO XML ,优化的重点还是在细节,细节决定成败一点也没错

    View Code
        System.Diagnostics.Stopwatch st1 = new System.Diagnostics.Stopwatch();
                st1.Start();
                XDocument xdom = XDocument.Load(XmlPath);
                var restElements = xdom.Descendants("rest").ToList();
                st1.Stop();
                HttpContext.Current.Response.Write("1.载入XML时间" + st1.ElapsedMilliseconds + "<br>");

    载入一个大一点的文件,我这里显示花时间 1700毫秒 左右

    View Code
               System.Diagnostics.Stopwatch st = new System.Diagnostics.Stopwatch();
                st.Start();
                var Hotels = (from hotels in restElements
                              where  
                              hotels.Element("lat") != null &&  
                              hotels.Element("lat").Value != string.Empty 
                              select new
                              {
                                  State = hotels.Element("state").Value,
                                  StateCityKey = (hotels.Element("state").Value + "-" + hotels.Element("city").Value).ToUpper()
                              }).ToList();

    过滤,,生成匿名对象,然后Tolist();这一步的没什么好说的,关键是不要让他监视XML文件,不要让他延时加载,生成一个KEY

    View Code
                var hotelsGroup =
                    (from hotel in Hotels
                     group hotel by hotel.StateCityKey
                         into h
                         select new
                         {
                             StateCityKey = h.Key,
                             count = h.Count(),
                             hgrou = h
                         } into c
                         where c.count > 2
                         select c).ToList();
                st.Stop();
                HttpContext.Current.Response.Write("2.分组耗费" + st.ElapsedMilliseconds);
                long a = st1.ElapsedMilliseconds + st.ElapsedMilliseconds;
                HttpContext.Current.Response.Write("<br>1+2总耗费 :" + a + "毫秒<br>");

     分组耗时:40毫秒

    通过KEY(StateCityKey)进行分组,注意COUNT,where c.count > 2 对分组以后的数据进行判断,相当于HAVING

    用的不是COUNT() 是count  ,

    下面的循环统计输出就不放出来了。。。

     转载请放链接 http://www.cnblogs.com/jacd/archive/2012/04/25/2469389.html

  • 相关阅读:
    LeetCode 109 Convert Sorted List to Binary Search Tree
    LeetCode 108 Convert Sorted Array to Binary Search Tree
    LeetCode 107. Binary Tree Level Order Traversal II
    LeetCode 106. Construct Binary Tree from Inorder and Postorder Traversal
    LeetCode 105. Construct Binary Tree from Preorder and Inorder Traversal
    LeetCode 103 Binary Tree Zigzag Level Order Traversal
    LeetCode 102. Binary Tree Level Order Traversal
    LeetCode 104. Maximum Depth of Binary Tree
    接口和多态性
    C# 编码规范
  • 原文地址:https://www.cnblogs.com/jacd/p/2469389.html
Copyright © 2011-2022 走看看