Linq101-Grouping Operators

时间:2023-03-09 09:16:50
Linq101-Grouping Operators
 using System;
using System.Collections.Generic;
using System.Linq; namespace Linq101
{
class Grouping
{
/// <summary>
/// This sample uses group by to partition a list of numbers by their remainder when divided by 5.
/// </summary>
public void Linq40()
{
int[] numbers = { , , , , , , , , , }; var numberGroups = from n in numbers
group n by n % into g
select new { Remainder = g.Key, Numbers = g }; foreach (var numberGroup in numberGroups)
{
Console.WriteLine("除以5余数为{0}的有:", numberGroup.Remainder);
foreach (var n in numberGroup.Numbers)
{
Console.WriteLine(n);
}
}
} /// <summary>
/// This sample uses group by to partition a list of words by their first letter.
/// </summary>
public void Linq41()
{
string[] words = { "blueberry", "chimpanzee", "abacus", "banana", "apple", "cheese" }; var wordGroups = from w in words
group w by w[] into g
select new { FirstLetter = g.Key, words = g }; foreach (var wordGroup in wordGroups)
{
Console.WriteLine("以字母{0}开头的单词有:", wordGroup.FirstLetter);
foreach (var word in wordGroup.words)
{
Console.WriteLine(word);
}
}
} /// <summary>
/// This sample uses group by to partition a list of products by category.
/// </summary>
public void Linq42()
{
var products = Data.GetProductList(); var productGroups = from p in products
group p by p.Category into g
select new { Category = g.Key, products = g }; //ObjectDumper.Write(productGroups,1); foreach (var productGroup in productGroups)
{
Console.WriteLine("分类为{0}的产品有:", productGroup.Category);
foreach (var product in productGroup.products)
{
ObjectDumper.Write(product);
}
}
} /// <summary>
/// This sample uses group by to partition a list of each customer's orders, first by year, and then by month.
/// </summary>
public void Linq43()
{
var customers = Data.GetCustomerList(); var customerOrderGroups = from c in customers
select new
{
c.CompanyName,
YearGroups = from o in c.Orders
group o by o.OrderDate.Year into yg
select new
{
Year = yg.Key,
MonthGoups = from o in yg
group o by o.OrderDate.Month into mg
select new { Month = mg.Key, mg }
}
}; ObjectDumper.Write(customerOrderGroups, );
} /// <summary>
/// This sample uses GroupBy to partition trimmed elements of an array using a custom comparer that matches words that are anagrams of each other.
/// </summary>
public void Linq44()
{
string[] anagrams = { "from ", " salt", " earn ", " last ", " near ", " form " }; var query = anagrams.GroupBy(w => w.Trim(), new AnagramEqualityComparer()); ObjectDumper.Write(query, );
} 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(wordChars);
return new string(wordChars);
}
} /// <summary>
/// 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.
/// </summary>
public void Linq45()
{
string[] anagrams = { "from ", " salt", " earn ", " last ", " near ", " form " }; var query = anagrams.GroupBy(w => w.Trim(),
a => a.ToUpper(),
new AnagramEqualityComparer()); ObjectDumper.Write(query, );
}
}
}