using System;
using System.Collections.Generic;
using System.Linq; namespace Linq101
{
class Set
{
/// <summary>
/// This sample uses Distinct to remove duplicate elements in a sequence of factors of 300.
/// </summary>
public void Linq46()
{
int[] factorsOf300 = { , , , , }; var uniqueFactors = factorsOf300.Distinct(); Console.WriteLine("300的因数:");
foreach (var factor in uniqueFactors)
{
Console.WriteLine(factor);
}
} /// <summary>
/// This sample uses Distinct to find the unique Category names.
/// </summary>
public void Linq47()
{
List<Data.Product> products = Data.GetProductList(); var categoryNames = (from p in products
select p.Category).Distinct(); Console.WriteLine("种类名称:");
ObjectDumper.Write(categoryNames);
} /// <summary>
/// This sample uses Union to create one sequence that contains the unique values from both arrays.
/// </summary>
public void Linq48()
{
int[] numbersA = { , , , , , , };
int[] numbersB = { , , , , }; var uniqueNumbers = numbersA.Union(numbersB); Console.WriteLine("Unique numbers from both arrays:");
ObjectDumper.Write(uniqueNumbers);
} /// <summary>
/// This sample uses Union to create one sequence that contains the unique first letter from both product and customer names.
/// </summary>
public void Linq49()
{
List<Data.Product> products = Data.GetProductList();
List<Data.Customer> customers = Data.GetCustomerList(); var productFirstChar = from p in products select p.ProductName[];
var customerFirstChar = from c in customers select c.CompanyName[]; var uniqueFirstChar = productFirstChar.Union(customerFirstChar); Console.WriteLine("Unique First Char:");
ObjectDumper.Write(uniqueFirstChar);
} /// <summary>
/// This sample uses Intersect to create one sequence that contains the common values shared by both arrays.
/// </summary>
public void Linq50()
{
int[] numbersA = { , , , , , , };
int[] numbersB = { , , , , }; var commonNumbers = numbersA.Intersect(numbersB); Console.WriteLine("共有的数字:");
ObjectDumper.Write(commonNumbers);
} /// <summary>
/// This sample uses Intersect to create one sequence that contains the common first letter from both product and customer names.
/// </summary>
public void Linq51()
{
List<Data.Product> products = Data.GetProductList();
List<Data.Customer> customers = Data.GetCustomerList(); var productFirstChar = from p in products select p.ProductName[];
var customerFirstChar = from c in customers select c.CompanyName[]; var commonFirstChar = productFirstChar.Intersect(customerFirstChar); Console.WriteLine("Common First Char");
ObjectDumper.Write(commonFirstChar);
} /// <summary>
/// This sample uses Except to create a sequence that contains the values from numbersAthat are not also in numbersB.
/// </summary>
public void Linq52()
{
int[] numbersA = { , , , , , , };
int[] numbersB = { , , , , }; var aOnlyNumbers = numbersA.Except(numbersB); Console.WriteLine("Numbers in arrayA but no ArrayB");
ObjectDumper.Write(aOnlyNumbers);
} /// <summary>
/// This sample uses Except to create one sequence that contains the first letters of product names that are not also first letters of customer names.
/// </summary>
public void Linq53()
{
List<Data.Product> products = Data.GetProductList();
List<Data.Customer> customers = Data.GetCustomerList(); var productFirstChar = from p in products select p.ProductName[];
var customerFirstChar = from c in customers select c.CompanyName[]; var productOnlyFirstChar = productFirstChar.Except(customerFirstChar); Console.WriteLine("First char only in productFirstChar");
ObjectDumper.Write(productOnlyFirstChar);
}
}
}