[Category("Grouping Operators")]
[Description("This sample uses group by to partition a list of numbers by " +
"their remainder when divided by 5.")]
public void DataSetLinq40()
{ var numbers = testDS.Tables["Numbers"].AsEnumerable();//testDS DataSet
foreach (var n in numbers)
{
Console.Write(n.Field<int>("number")+",");
}
Console.WriteLine();
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"));
}
}
Console.ReadLine();
}

运行结果(按照数字求余5的值分组):

             [Category("Grouping Operators")]
[Description("This sample uses group by to partition a list of words by " +
"their first letter.")]
public void DataSetLinq41()
{ var words4 = testDS.Tables["Words4"].AsEnumerable();
foreach (var g in words4)
{
Console.Write(g.Field<string>("word")+",");
}
Console.WriteLine();
var wordGroups =
from w in words4
group w by w.Field<string>("word")[] into g
select new { FirstLetter = g.Key, Words = g }; foreach (var g in wordGroups)
{
Console.WriteLine("Words that start with the letter '{0}':", g.FirstLetter);
foreach (var w in g.Words)
{
Console.WriteLine(w.Field<string>("word"));
}
}
Console.ReadLine();
}

运行结果(按照首字母分组):

             [Category("Grouping Operators")]
[Description("This sample uses group by to partition a list of products by category.")]
public void DataSetLinq42()
{ var products = testDS.Tables["Products"].AsEnumerable(); var productGroups =
from p in products
group p by p.Field<string>("Category") into g
select new { Category = g.Key, Products = g }; foreach (var g in productGroups)
{
Console.WriteLine("Category: {0}", g.Category);
foreach (var w in g.Products)
{
Console.WriteLine("\t" + w.Field<string>("ProductName"));
}
}
Console.ReadLine();
}

运行结果(按照产品类别分组):

             [Category("Grouping Operators")]
[Description("This sample uses group by to partition a list of each customer's orders, " +
"first by year, and then by month.")]
public void DataSetLinq43()
{ var customers = testDS.Tables["Customers"].AsEnumerable(); var customerOrderGroups =
from c in customers
select
new
{
CompanyName = c.Field<string>("CompanyName"),
YearGroups =
from o in c.GetChildRows("CustomersOrders")
group o by o.Field<DateTime>("OrderDate").Year into yg
select
new
{
Year = yg.Key,
MonthGroups =
from o in yg
group o by o.Field<DateTime>("OrderDate").Month into mg
select new { Month = mg.Key, Orders = mg }
}
}; foreach (var cog in customerOrderGroups)
{
Console.WriteLine("CompanyName= {0}", cog.CompanyName);
foreach (var yg in cog.YearGroups)
{
Console.WriteLine("\t Year= {0}", yg.Year);
foreach (var mg in yg.MonthGroups)
{
Console.WriteLine("\t\t Month= {0}", mg.Month);
foreach (var order in mg.Orders)
{
Console.WriteLine("\t\t\t OrderID= {0} ", order.Field<int>("OrderID"));
Console.WriteLine("\t\t\t OrderDate= {0} ", order.Field<DateTime>("OrderDate"));
}
}
}
}
Console.ReadLine();
}

DataTable:

运行结果(对每个客户的订单按年再按月分组):

 [Category("Grouping Operators")]
[Description("This sample uses GroupBy to partition trimmed elements of an array using " +
"a custom comparer that matches words that are anagrams of each other.")]
public void DataSetLinq44()
{ 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"));
}
}
} [Category("Grouping Operators")]
[Description("This sample uses GroupBy to partition trimmed elements of an array using " +
"a custom comparer that matches words that are anagrams of each other, " +
"and then converts the results to uppercase.")]
public void DataSetLinq45()
{ var anagrams = testDS.Tables["Anagrams"].AsEnumerable(); var orderGroups = anagrams.GroupBy(
w => w.Field<string>("anagram").Trim(),
a => a.Field<string>("anagram").ToUpper(),
new AnagramEqualityComparer()
); foreach (var g in orderGroups)
{
Console.WriteLine("Key: {0}", g.Key);
foreach (var w in g)
{
Console.WriteLine("\t" + w);
}
}
}

内容源自:http://code.msdn.microsoft.com/101-LINQ-Samples-3fb9811b

Linq:Grouping Operators的更多相关文章

  1. Linq编程101例

    原文地址:101 LINQ Samples in C# Part1 - Restriction Operators Part2 - Projection Operators Part3 - Parti ...

  2. 101个LINQ示例,包含几乎全部操作

    Restriction Operators Where - Simple public void Linq1() { , , , , , , , , , }; var lowNums = from n ...

  3. LINQ 学习路程 -- 查询操作 GroupBy ToLookUp

    Grouping Operators Description GroupBy GroupBy操作返回根据一些键值进行分组,每组代表IGrouping<TKey,TElement>对象 To ...

  4. Flume interceptor 使用注意事项

    1. 在使用 Regex Filtering Interceptor的时候一个属性是excludeEvents 当它的值为true 的时候,过滤掉匹配到当前正则表达式的一行 当它的值为false的时候 ...

  5. 【翻译】Flume 1.8.0 User Guide(用户指南) Processors

    翻译自官网flume1.8用户指南,原文地址:Flume 1.8.0 User Guide 篇幅限制,分为以下5篇: [翻译]Flume 1.8.0 User Guide(用户指南) [翻译]Flum ...

  6. tfs二次开发-利用tfs api做查询

    参考地址:https://msdn.microsoft.com/en-us/library/bb130306(v=vs.120).aspx You can query for bugs, tasks, ...

  7. Threading in C# 5

    Part 5: Parallel Programming In this section, we cover the multithreading APIs new to Framework 4.0 ...

  8. Rx (Reactive Extensions)

    The Reactive Extensions (Rx) is a library for composing asynchronous and event-based programs using ...

  9. matlab 正则表达式

    regexprep Replace text using regular expression collapse all in page Syntax newStr = regexprep(str,e ...

随机推荐

  1. CSS 初级攻略

    内容来自html dog. css的格式为 ‘property: value’ 给html插入css样式的方式有三种:内联.内部css.外部css文件,如下所示: <p style=" ...

  2. Linux关于yum命令Error: Cannot retrieve repository metadata (repomd.xml) for repository:xxxxxx.

    Linux关于yum命令Error: Cannot retrieve repository metadata (repomd.xml) for repository:xxxxxx. 问题: Linux ...

  3. Nginx 自定义404、500错误页面跳转

    自定义Nginx错误界面跳转 1.开启Nginx.conf配置文件下的自定义接口参数. http { fastcgi_intercept_errors on; } 2.在Server区域添加自定义的错 ...

  4. CSS3鼠标悬停边框线条动画按钮

    在线演示 本地下载

  5. JSONObject使用方法

    转载:http://blog.csdn.net/dongzhouzhou/article/details/8664569 1.JSONObject介绍 JSONObject-lib包是一个beans, ...

  6. 全卷积网络(FCN)与图像分割

    最近在做物体检测,也用到了全卷积网络,来此学习一波. 这篇文章写了很好,有利于入门,在此记录一下: http://blog.csdn.net/taigw/article/details/5140144 ...

  7. C# word 类库基本属性介绍

    using System;using System.Collections.Generic;using System.Text;using Microsoft.Office.Interop.Word; ...

  8. spark学习10(win下利用Intellij IDEA搭建spark开发环境)

    第一步:启动IntelliJ IDEA,选择Create New Project,然后选择Scala,点击下一步,输入项目名称wujiadong.spark继续下一步 第二步:导入spark-asse ...

  9. showdoc.js代码

    //页面加载完就执行 $(function(){ //自动根据url把当前菜单激活 var page_id = GetQueryString('page_id'); //如果中没有指定page_id, ...

  10. js中的数组对象中的方法解析

    concat()方法:  合并两个数组,返回新对象的结果: join()方法 :  把数组内的所有元素加入到一个字符串中,传入的分隔符就是指定的分隔符 pop()方法: 删除数组并返回数组的最后一个元 ...