linq group by having 实现

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

 

 

ExpandedBlockStart.gif 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毫秒 左右

 

 

ExpandedBlockStart.gif 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

 

ExpandedBlockStart.gif 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

转载于:https://www.cnblogs.com/jacd/archive/2012/04/25/2469389.html

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

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

余额充值