1.lin语句

 int[] nums = { 5, 4, 1, 3, 9, 8, 6, 7, 2, 0, 3 };
DataTable table = new DataTable("Numbers");
table.Columns.Add("number", typeof(int));
foreach (int n in nums)
{
table.Rows.Add(new object[] { n });
} var numbers = table.AsEnumerable();
var numberGroups =
from n in numbers
group n by n.Field<int>("number") into g
select new { Remainder = g.Key, Numbers = g }; foreach (var g in numberGroups)
{
Console.WriteLine("Numbers with a remainder of {0} when divided by 5:", g.Remainder);
foreach (var n in g.Numbers)
{
Console.WriteLine(n.Field<int>("number") + ",count:" + g.Numbers.Count());
}
} 2.比较器
public void Method{
var anagrams = testDS.Tables["Anagrams"].AsEnumerable(); var orderGroups = anagrams.GroupBy(w => w.Field<string>("anagram").Trim(), new AnagramEqualityComparer()); foreach (var g in orderGroups)
{
Console.WriteLine("Key: {0}", g.Key);
foreach (var w in g)
{
Console.WriteLine("\t" + w.Field<string>("anagram"));
}
}
} //创建一个比较器 private class AnagramEqualityComparer : IEqualityComparer<string>
{
public bool Equals(string x, string y)
{
return getCanonicalString(x) == getCanonicalString(y);
} public int GetHashCode(string obj)
{
return getCanonicalString(obj).GetHashCode();
} private string getCanonicalString(string word)
{
char[] wordChars = word.ToCharArray();
Array.Sort<char>(wordChars);
return new string(wordChars);
}
}

  

Linq分组的更多相关文章

  1. Linq分组功能

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

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

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

  3. Linq分组,linq方法分组

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

  4. Linq分组查询统计

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

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

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

  6. c# linq 分组groupby

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

  7. linq分组求和_实体类和datatable

    1.数据分组求合,分别用的实体类以及datatable来分组求合,还有分组求和之后的如何取值 //实体类版本 List<ProgramTimeModel> TotalAllList = G ...

  8. 示例, linq分组

    public class HIS_CLIREGISTER : BaseModel{ private String _FBCODE;[StringLength(8)]/// <summary> ...

  9. LINQ分组取出第一条数据

    Person1: Id=, Name="Test1" Person2: Id=, Name="Test1" Person3: Id=, Name="T ...

  10. C# DataTable 通过Linq分组

    datatable我们是经常使用到的,但是需要对数据进行分组,具体代码如下: var result = dt.AsEnumerable().GroupBy(f => new { type = f ...

随机推荐

  1. java.lang.IllegalStateException: getOutputStream() has already been called for this response

    ERROR [Engine] StandardWrapperValve[jsp]: Servlet.service() for servlet jsp threw exceptionjava.lang ...

  2. 同步异步,阻塞非阻塞 和nginx的IO模型

    同步与异步 同步和异步关注的是消息通信机制 (synchronous communication/ asynchronous communication).所谓同步,就是在发出一个*调用*时,在没有得 ...

  3. userdel 连同家目录一起删除

    userdel -r xxx 连同家目录一起删除

  4. Pyqt SpVoice朗读功能

    用Pyqt 做一个读取系统剪贴板内容,然后通过语音合成(TTS)朗读出剪贴板的内容 知识要点 SpVoice SpVoice类是支持语音合成(TTS)的核心类.通过SpVoice对象调用TTS引擎,从 ...

  5. 【leetcode】Spiral Matrix

    题目概要: Given a matrix of m x n elements (m rows, n columns), return all elements of the matrix in spi ...

  6. [JAVA] BlockingQueue学习

    有点时间,巩固巩固下基础知识:BlockingQueue,如果BlockQueue是空的,从BlockingQueue取东西的操作将会被阻断进入等待状态,直到BlockingQueue进了东西才会被唤 ...

  7. sql表和字段的别名

    1. sql表和字段的别名通过关键字 AS 来指定. 2.通常,定义字段别名的 AS 关键字可以省略,但我们建议不要省略 AS 关键字.别名(alias)是 SQL 的标准语法,几乎所有的数据库系统都 ...

  8. 宿主机ping不通虚拟机cenos7

    参考网址1:http://zhidao.baidu.com/link?url=2v3NXGyzPT-XTYwon8PesZLnMg02Ako6nDub3vJiJt4miSmkOA-04xLUqfu9s ...

  9. c++2008 并行配置文件和获取字典的所有key的方法

    1 需要 在官网 下载对应的执行包... 2, # !/usr/bin/python3.4 # -*- coding: utf-8 -*- b = { 'video':0, 'music':23 } ...

  10. 我的c++学习(10)this指针

    问题:当在对象的外部访问该对象的公有成员时,必须指明是哪一个对象.但是当我们用对象的成员函数来访问本对象的成员时,在成员函数中只要给出成员名就可以实现对该对象成员的访问.再进一步可用同一个类创建很多个 ...