101个LINQ例子 101 LINQ Samples
Restriction Operators Where - Simple 1
public void Linq1() {
int[] numbers = { 5, 4, 1, 3, 9, 8, 6, 7, 2, 0 }; var lowNums =
from n in numbers where n < 5 select n;
Console.WriteLine(\ foreach (var x in lowNums) { Console.WriteLine(x); } }
Where - Simple 2 public void Linq2() {
List products = GetProductList(); var soldOutProducts =
from p in products
where p.UnitsInStock == 0 select p;
Console.WriteLine(\
foreach (var product in soldOutProducts) {
Console.WriteLine(\ } }
Where - Simple 3 public void Linq3() {
List products = GetProductList(); var expensiveInStockProducts =
from p in products
where p.UnitsInStock > 0 && p.UnitPrice > 3.00M select p;
Console.WriteLine(\ foreach (var product in expensiveInStockProducts) {
Console.WriteLine(\ }
}
Where - Drilldown
public void Linq4() {
List customers = GetCustomerList(); var waCustomers =
from c in customers where c.Region == \
select c;
Console.WriteLine(\ foreach (var customer in waCustomers) { Console.WriteLine(\customer.CompanyName);
foreach (var order in customer.Orders) {
Console.WriteLine(\.OrderDate); } } }
Where - Indexed public void Linq5() {
string[] digits = { \ var shortDigits = digits.Where((digit, index) => digit.Length < index); Console.WriteLine(\
foreach (var d in shortDigits) {
Console.WriteLine(\ } }
Projection Operators
Select - Simple 1 public void Linq6() {
int[] numbers = { 5, 4, 1, 3, 9, 8, 6, 7, 2, 0 }; var numsPlusOne = from n in numbers
select n + 1;
Console.WriteLine(\ foreach (var i in numsPlusOne) { Console.WriteLine(i); } }
Select - Simple 2 public void Linq7() {
List products = GetProductList(); var productNames = from p in products
{0}:
{1}\
customer.CustomerID,
select p.ProductName;
Console.WriteLine(\
foreach (var productName in productNames) { Console.WriteLine(productName); } }
Select - Transformation public void Linq8() {
int[] numbers = { 5, 4, 1, 3, 9, 8, 6, 7, 2, 0 };
string[] strings = { \\\\\\\\\\
var textNums =
from n in numbers
select strings[n];
Console.WriteLine(\ foreach (var s in textNums) { Console.WriteLine(s); } }
Select - Anonymous Types 1 public void Linq9() {
string[] words = { \ var upperLowerWords =
from w in words
select new {Upper = w.ToUpper(), Lower = w.ToLower()};
foreach (var ul in upperLowerWords) {
Console.WriteLine(\ } }
Select - Anonymous Types 2 public void Linq10() {
int[] numbers = { 5, 4, 1, 3, 9, 8, 6, 7, 2, 0 };
string[] strings = { \\\\\\\\\\
var digitOddEvens = from n in numbers
select new {Digit = strings[n], Even = (n % 2 == 0)}; foreach (var d in digitOddEvens) {
Console.WriteLine(\ } }
Select - Anonymous Types 3 public void Linq11() {
List products = GetProductList(); var productInfos =
from p in products
select new {p.ProductName, p.Category, Price = p.UnitPrice}; Console.WriteLine(\
foreach (var productInfo in productInfos) {
Console.WriteLine(\is in the category {1} and costs {2} per unit.\productInfo.ProductName, productInfo.Category, productInfo.Price); } }
Select - Indexed public void Linq12() {
int[] numbers = { 5, 4, 1, 3, 9, 8, 6, 7, 2, 0 };
var numsInPlace = numbers.Select((num, index) => new {Num = num, InPlace = (num == index)});
Console.WriteLine(\ foreach (var n in numsInPlace) {
Console.WriteLine(\ } }
Select - Filtered public void Linq13() {
int[] numbers = { 5, 4, 1, 3, 9, 8, 6, 7, 2, 0 };
string[] digits = { \ var lowNums = from n in numbers where n < 5
select digits[n];
Console.WriteLine(\ foreach (var num in lowNums) { Console.WriteLine(num); } }
SelectMany - Compound from 1 public void Linq14() {
int[] numbersA = { 0, 2, 4, 5, 6, 8, 9 }; int[] numbersB = { 1, 3, 5, 7, 8 }; var pairs =
from a in numbersA,
b in numbersB where a < b
select new {a, b};
Console.WriteLine(\
foreach (var pair in pairs) {
Console.WriteLine(\.b); } }
SelectMany - Compound from 2 public void Linq15() {
List customers = GetCustomerList(); var orders =
from c in customers, o in c.Orders
where o.Total < 500.00M
select new {c.CustomerID, o.OrderID, o.Total}; ObjectDumper.Write(orders); }
SelectMany - Compound from 3 public void Linq16() {
List customers = GetCustomerList(); var orders =
from c in customers, o in c.Orders
where o.OrderDate >= new DateTime(1998, 1, 1) select new {c.CustomerID, o.OrderID, o.OrderDate}; ObjectDumper.Write(orders); }
SelectMany - from Assignment public void Linq17() {
List customers = GetCustomerList(); var orders =
from c in customers, o in c.Orders, total = o.Total
where total >= 2000.0M
select new {c.CustomerID, o.OrderID, total}; ObjectDumper.Write(orders); }