Linq在集合操作上很方便,很多语法都借鉴自sql,但linq的分组却与sql有一定的区别,故整理发布如下。 1.  Linq分组 分组后以Key属性访问分组键值。 每一组为一个IEnumberAble或IQeuryAble的集合,可以继续枚举。 Sample:


 string[] World = { "Hello","World"}; string[] Brother = { "Hello","Brother"}; var result = from wld in World from bth in Brother group new { wld, bth } by new { wld, bth }.bth; OR var result = from wld in World from bth in Brother group new { wld, bth } by bth;
foreach (var obj in result) { foreach (var ct in obj) { Response.Write(ct.wld + ct.bth); } }

说明:在linq里面,group by 和groupby是两个不同的概念,前者标识分组,后者标识排序。分组时如不特意制定select,则将分组后的结果作为结果集。

2.   Linq排序后,分组再排序


    public class Student      {          public string Name = "";          public string Sex = "";          public int Age;      }
Student[] stAry = { new Student{ Name ="Jon1",Sex="female",Age =21}, new Student{ Name ="Jon2",Sex="male",Age =22}, new Student{ Name ="Jon3",Sex="male",Age =33}, new Student{ Name ="Jon4s",Sex="female",Age =44}, new Student{ Name ="Jon5",Sex="female",Age =25}, new Student{ Name ="Jon6",Sex="female",Age =26} }; var c = from st in stAry orderby st.Age group st by st.Sex into temp orderby temp.Key ascending select temp;

概要说明: Linq查询后产生的结果集和sql类似,如果涉及到多个查询则会产生一个多“列”的集合用以“select”,如果用到linq的分组功能,则分组后的结果将作为一个“集合”,而这个集合可以在他所在的查询中作为一个源被查询,也可以当做一个元素被直接“select”。如下例所示。注意:Into在linq里面也用以分组,产生的结果用来查询,当然,不用这个结果也不会错。

3.  join分组:

        int[] Ary1 = { 1, 23, 45, 67, 8, 4, 4 };          int[] Ary2 = { 23, 1, 1,5, 67, 4 };
var c = from A1 in Ary1 join A2 in Ary2 on A1 equals A2 into VAL2 select VAL2;

左外连接:

        var c = from A1 in Ary1                  join A2 in Ary2 on A1 equals A2 into VAL2                  from a2 in VAL2. DefaultIfEmpty(int.MinValue)                   select new {A1,a2,VAL2};

注:DefaultIfEmpty(int.MinValue)表示集合为空的时候,用指定的默认值关联前面的集合。如果不指定,则由于集合为空,A1找不到关联的对象将不产生相应的行。

4.linq嵌套查询   查询某个数据集中不全为null或“”的列

            var c = from DataColumn dc in ds.Tables[0].Columns                      where ( from dr in dt.AsEnumerable()                              where !dr.IsNull(dc)                                  select dr                                  ).Any()                      select dc;

5.分组后统计

                var groupedApply901 = (from y in arrInput
group y by new { y.UnavailableType, y.FlowType } into z
select new CashBagUnavailableShare
{
UnavailableType = z.Key.UnavailableType,
FlowType = z.Key.FlowType,
UnavailableVol = z.Sum(s => s.UnavailableVol), }).ToList<CashBagUnavailableShare>();

 转一下自己的文章

Linq分组功能的更多相关文章

  1. 在 Students 的 Index 页面增加列标题链接(排序),分页,过滤和分组功能

    3-1  在 Students 的 Index 页面增加列标题链接 为 Index 页面增加排序的功能,我们需要修改 Student 控制器的 Index 方法,还需要为 Student 视图增加代码 ...

  2. 扩展GridView控件——为内容项添加拖放及分组功能

    引言 相信大家对GridView都不陌生,是非常有用的控件,用于平铺有序的显示多个内容项.打开任何WinRT应用或者是微软合作商的网站,都会在APP中发现GridView的使用.“Tiles”提供了一 ...

  3. iOS开发——UI篇Swift篇&玩转UItableView(三)分组功能

    UItableView分组功能 class UITableViewControllerGroup: UIViewController, UITableViewDataSource, UITableVi ...

  4. Linq分组操作之GroupBy,GroupJoin扩展方法源码分析

    Linq分组操作之GroupBy,GroupJoin扩展方法源码分析 一. GroupBy 解释: 根据指定的键选择器函数对序列中的元素进行分组,并且从每个组及其键中创建结果值. 查询表达式: var ...

  5. SQL——连接查询、聚合函数、开窗函数、分组功能、联合查询、子查询

    连接查询 inner join,用的最多,表示多张表一一对应 聚合函数 操作行数据,进行合并 sum.avg.count.max.min 开窗函数 将合并的数据分布到原表的每一行,相当于多出来了一列, ...

  6. Linq分组,linq方法分组

    Group在SQL经常使用,通常是对一个字段或者多个字段分组,求其总和,均值等. Linq中的Groupby方法也有这种功能.具体实现看代码: 假设有如下的一个数据集: 01.public class ...

  7. Linq分组查询统计

    这里介绍Linq使用Group By和Count得到每个CategoryID中产品的数量,Linq使用Group By和Count得到每个CategoryID中断货产品的数量等方面. 学经常会遇到Li ...

  8. C# 泛型分组和Linq分组的异同

    没什么好说的,因为用的到,所以作个记录, 代码如下: using System; using System.Collections.Generic; using System.Linq; using ...

  9. c# linq 分组groupby

    转载: https://www.cnblogs.com/cncc/p/9846390.html 一.先准备要使用的类: 1.Person类: class Person { public string ...

随机推荐

  1. 解决Windows 10下Wireshark运行问题

    解决Windows 10下Wireshark运行问题在Windows 10下,安装Wireshark时候,提示WinPcap不被系统系统支持.这是由于最新版的WinPcap 4.1.3只支持到Wind ...

  2. Codeforces Round #293 (Div. 2)

    A. Vitaly and Strings 题意:两个字符串s,t,是否存在满足:s < r < t 的r字符串 字符转处理:字典序排序 很巧妙的方法,因为s < t,只要找比t字典 ...

  3. LightOJ1119 Pimp My Ride(状压DP)

    dp[S]表示已经完成的工作集合 枚举从哪儿转移过来的,再通过枚举计算花费..水水的.. #include<cstdio> #include<cstring> #include ...

  4. Rain on your Parade

    Rain on your Parade Time Limit: 6000/3000 MS (Java/Others)    Memory Limit: 655350/165535 K (Java/Ot ...

  5. 【转】Profiling application LLC cache misses under Linux using Perf Events

    转自:http://ariasprado.name/2011/11/30/profiling-application-llc-cache-misses-under-linux-using-perf-e ...

  6. 自己的一份Spring的xml配置文件

    <?xml version="1.0" encoding="UTF-8"?><beans xmlns="http://www.spr ...

  7. UIView 周围出现黑线的解决方法

    myView.clipsToBounds = YES;

  8. 【POJ】2406 Power Strings

    http://poj.org/problem?id=2406 题意:给定一个字符串 L,已知这个字符串是由某个字符串 S 重复 R 次而得到的,求 R 的最大值.(长度<=1000000) #i ...

  9. c++ map 的使用

    1.map是一类关联式容器,它是模板类. 关联的本质在于元素的值与某个特定的键相关联,而并非通过元素在数组中的位置类获取.它的特点是增加和删除节点对迭代器的影响很小,除了操作节点,对其他的节点都没有什 ...

  10. c#委托事件及其讲解

    一定要标明出处,波哥的文章.所有文章都值得一看.这篇是摘抄的大白话之C#事件讲解.委托 http://www.cnblogs.com/wudiwushen/archive/2010/04/20/170 ...