[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. ASP.NET MVC Bootstrap模板选中菜单高亮显示当前项方法

    当我们处理后台显示当前页面,当前页菜单项高亮,我们可以使用js方法,也可用程序实现,使用Bootstrap模板处理高亮并展开方法之一 1.在项目中导入 <script src="/as ...

  2. zabbix监控使用

    zabbix监控 通过导入/导出zabbix配置文件,我们可以将自己写好的模板等配置在网络上分享,我们也可以导入网络上分享的配置文件,配置文件有两种格式,分为xml与json,通过zabbix管理界面 ...

  3. CentOS7/6 关闭防火墙

    CentOS6关闭防火墙使用以下命令, //临时关闭 service iptables stop //禁止开机启动 chkconfig iptables off CentOS7中若使用同样的命令会报错 ...

  4. web前端调优

    web前端性能调优(二) http://www.cnblogs.com/liaoweipeng/p/6306606.html

  5. JQuery -- Validate, Jquery 表单校验

    1. Jquery 表单验证需要插件 jQuery validation 1.7  ---验证插件需要:jQuery 1.3.2 或 1.4.2版本 http://jquery.bassistance ...

  6. NumPy副本和视图

    NumPy - 副本和视图 在执行函数时,其中一些返回输入数组的副本,而另一些返回视图. 当内容物理存储在另一个位置时,称为副本. 另一方面,如果提供了相同内存内容的不同视图,我们将其称为视图. 无复 ...

  7. js 小复习1

    1.数组 增删改查 , , , , ]; // arr.push(18); // 添加数组后面 更改长度 // arr.unshift(12); // 添加数组前面 // arr.pop(); // ...

  8. python 实现简单点名程序

    程序会遍历文件所有姓名,遍历完之前不会有重复值,遍历所有后将提示推出. #-*-coding:utf-8-*-#author:wangxing #点名程序 import randomimport os ...

  9. WPF的外观装饰类——Border

    public class Border : System.Windows.Controls.Decorator 说明:在另一个元素的周围绘制边框.背景或同时绘制二者.

  10. 状态管理(Vuex、 Flux、Redux、The Elm Architecture)

    1.https://vuex.vuejs.org/zh-cn/intro.html (vuex) 这就是 Vuex 背后的基本思想,借鉴了 Flux.Redux.和 The Elm Architect ...