[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. jQuery 3D垂直多级菜单

    在线演示 本地下载

  2. <nginx+PHP>nginx环境下配置支持php7

    [root@redhat7 ~]# wget http://am1.php.net/get/php-7.1.2.tar.gz/from/this/mirror [root@redhat7 ~]# ta ...

  3. Linux系统下chkconfig命令使用详解

    chkconfig命令可以用来检查.设置系统的各种服务 使用语法:chkconfig [--add][--del][--list][系统服务] 或 chkconfig [--level <等级代 ...

  4. SDOI2019&十二省联考 游记

    差不多写完了,然鹅去长郡学习前忘在机房电脑里了 总之是咕了

  5. 关于Android中根据ID名动态获取资源的两个方法

    在开发中, 我们习惯了类似下面这种方式去实现引用资源: context.getResources().getDrawable(R.drawable.flower); 但是,当我们提前知道这个资源的id ...

  6. Entity Framework 6:专家版本

    随着 Entity Framework 最新主版本 EF6 的推出,Microsoft 对象关系映射 (ORM) 工具达到了新的专业高度,与久负盛名的 .NET ORM 工具相比已不再是门外汉. EF ...

  7. Java小程序之输出星号

    题目:打印出如下图案(菱形)     *    ***  ****** ********  ******   ***    * 编程工具使用eclipse 代码如下: package test; pu ...

  8. 51nod-1259-分块+dp

    http://www.51nod.com/onlineJudge/questionCode.html#!problemId=1259 1259 整数划分 V2 基准时间限制:1 秒 空间限制:1310 ...

  9. ComBox绑定枚举

    (转自:http://blog.csdn.net/chao88552828/article/details/9903159) /// <summary> /// 参数枚举 /// < ...

  10. Struts05---动态查询

    01.在上面案例的login.jsp页面新增 <%-- 2.动态方法的调用 前提是在 struts.xml文件中开启 不推荐! --%> <a href="user/use ...