文档库 最新最全的文档下载
当前位置:文档库 › LINQ实例

LINQ实例

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("Numbers < 5:");

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("Sold out products:");

foreach (var product in soldOutProducts) {

Console.WriteLine("{0} is sold out!", product.ProductName);

}

}

?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("In-stock products that cost more than 3.00:");

foreach (var product in expensiveInStockProducts) {

Console.WriteLine("{0} is in stock and costs more than 3.00.", product.ProductName);

}

}

Where - Drilldown

public void Linq4() {

List customers = GetCustomerList();

var waCustomers =

from c in customers

where c.Region == "WA"

select c;

Console.WriteLine("Customers from Washington and their orders:");

foreach (var customer in waCustomers) {

Console.WriteLine("Customer {0}: {1}", customer.CustomerID, https://www.wendangku.net/doc/b44658342.html,panyName);

foreach (var order in customer.Orders) {

Console.WriteLine(" Order {0}: {1}", order.OrderID, order.OrderDate);

}

}

}

?Where - Indexed

public void Linq5() {

string[] digits = { "zero", "one", "two", "three", "four", "five", "six", "seven", "eight", "nine" };

var shortDigits = digits.Where((digit, index) => digit.Length < index);

Console.WriteLine("Short digits:");

foreach (var d in shortDigits) {

Console.WriteLine("The word {0} is shorter than its value.", d);

}

}

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("Numbers + 1:");

foreach (var i in numsPlusOne) {

Console.WriteLine(i);

}

}

?Select - Simple 2

public void Linq7() {

List products = GetProductList();

var productNames =

from p in products

select p.ProductName;

Console.WriteLine("Product Names:");

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 = { "zero", "one", "two", "three", "four", "five", "six", "seven", "eight", "nine" };

var textNums =

from n in numbers

select strings[n];

Console.WriteLine("Number strings:");

foreach (var s in textNums) {

Console.WriteLine(s);

}

}

Select - Anonymous Types 1

public void Linq9() {

string[] words = { "aPPLE", "BlUeBeRrY", "cHeRry" };

var upperLowerWords =

from w in words

select new {Upper = w.ToUpper(), Lower = w.ToLower()};

foreach (var ul in upperLowerWords) {

Console.WriteLine("Uppercase: {0}, Lowercase: {1}", ul.Upper, ul.Lower);

}

}

?Select - Anonymous Types 2

public void Linq10() {

int[] numbers = { 5, 4, 1, 3, 9, 8, 6, 7, 2, 0 };

string[] strings = { "zero", "one", "two", "three", "four", "five", "six", "seven", "eight", "nine" };

var digitOddEvens =

from n in numbers

select new {Digit = strings[n], Even = (n % 2 == 0)};

foreach (var d in digitOddEvens) {

Console.WriteLine("The digit {0} is {1}.", d.Digit, d.Even ? "even" : "odd");

}

}

?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("Product Info:");

foreach (var productInfo in productInfos) {

Console.WriteLine("{0} 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("Number: In-place?");

foreach (var n in numsInPlace) {

Console.WriteLine("{0}: {1}", n.Num, n.InPlace);

}

}

?Select - Filtered

public void Linq13() {

int[] numbers = { 5, 4, 1, 3, 9, 8, 6, 7, 2, 0 };

string[] digits = { "zero", "one", "two", "three", "four", "five", "six", "seven", "eight", "nine" };

var lowNums =

from n in numbers

where n < 5

select digits[n];

Console.WriteLine("Numbers < 5:");

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("Pairs where a < b:");

foreach (var pair in pairs) {

Console.WriteLine("{0} is less than {1}", pair.a, pair.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);

}

?SelectMany - Multiple from

public void Linq18() {

List customers = GetCustomerList();

DateTime cutoffDate = new DateTime(1997, 1, 1);

var orders =

from c in customers

where c.Region == "WA"

from o in c.Orders

where o.OrderDate >= cutoffDate

select new {c.CustomerID, o.OrderID};

ObjectDumper.Write(orders);

}

?SelectMany - Indexed

public void Linq19() {

List customers = GetCustomerList();

var customerOrders =

customers.SelectMany(

(cust, custIndex) =>

cust.Orders.Select(o => "Customer #" + (custIndex + 1) +

" has an order with OrderID " + o.OrderID) );

ObjectDumper.Write(customerOrders);

}

Partitioning Operators

?Take - Simple

public void Linq20() {

int[] numbers = { 5, 4, 1, 3, 9, 8, 6, 7, 2, 0 };

var first3Numbers = numbers.Take(3);

Console.WriteLine("First 3 numbers:");

foreach (var n in first3Numbers) {

Console.WriteLine(n);

}

}

?Take - Nested

public void Linq21() {

List customers = GetCustomerList();

var first3WAOrders = (

from c in customers

from o in c.Orders

where c.Region == "WA"

select new {c.CustomerID, o.OrderID, o.OrderDate} ) .Take(3);

Console.WriteLine("First 3 orders in WA:");

foreach (var order in first3WAOrders) {

ObjectDumper.Write(order);

}

}

Skip - Simple

public void Linq22() {

int[] numbers = { 5, 4, 1, 3, 9, 8, 6, 7, 2, 0 };

var allButFirst4Numbers = numbers.Skip(4);

Console.WriteLine("All but first 4 numbers:");

foreach (var n in allButFirst4Numbers) {

Console.WriteLine(n);

}

}

?Skip - Nested

public void Linq23() {

List customers = GetCustomerList();

var waOrders =

from c in customers

from o in c.Orders

where c.Region == "WA"

select new {c.CustomerID, o.OrderID, o.OrderDate};

var allButFirst2Orders = waOrders.Skip(2);

Console.WriteLine("All but first 2 orders in WA:");

foreach (var order in allButFirst2Orders) {

ObjectDumper.Write(order);

}

}

?TakeWhile - Simple

public void Linq24() {

int[] numbers = { 5, 4, 1, 3, 9, 8, 6, 7, 2, 0 };

var firstNumbersLessThan6 = numbers.TakeWhile(n => n < 6);

Console.WriteLine("First numbers less than 6:");

foreach (var n in firstNumbersLessThan6) {

Console.WriteLine(n);

}

}

?SkipWhile - Simple

public void Linq26() {

int[] numbers = { 5, 4, 1, 3, 9, 8, 6, 7, 2, 0 };

var allButFirst3Numbers = numbers.SkipWhile(n => n % 3 != 0);

Console.WriteLine("All elements starting from first element divisible by 3:");

foreach (var n in allButFirst3Numbers) {

Console.WriteLine(n);

}

}

?SkipWhile - Indexed

public void Linq27() {

int[] numbers = { 5, 4, 1, 3, 9, 8, 6, 7, 2, 0 };

var laterNumbers = numbers.SkipWhile((n, index) => n >= index);

Console.WriteLine("All elements starting from first element less than its position:");

foreach (var n in laterNumbers) {

Console.WriteLine(n);

}

}

Ordering Operators

OrderBy - Simple 1

publicvoid Linq28() {

string[] words = { "cherry", "apple", "blueberry" };

var sortedWords =

from w in words

orderby w

select w;

Console.WriteLine("The sorted list of words:");

foreach (var w in sortedWords) {

Console.WriteLine(w);

}

}

?OrderBy - Simple 2

public void Linq29() {

string[] words = { "cherry", "apple", "blueberry" };

var sortedWords =

from w in words

orderby w.Length

select w;

Console.WriteLine("The sorted list of words (by length):");

foreach (var w in sortedWords) {

Console.WriteLine(w);

}

}

?OrderBy - Simple 3

public void Linq30() {

List products = GetProductList();

var sortedProducts =

from p in products

orderby p.ProductName

select p;

ObjectDumper.Write(sortedProducts);

}

OrderBy - Comparer

public class CaseInsensitiveComparer : IComparer

{

public int Compare(string x, string y)

{

return https://www.wendangku.net/doc/b44658342.html,pare(x, y, true);

}

}

public void Linq31() {

string[] words = { "aPPLE", "AbAcUs", "bRaNcH", "BlUeBeRrY", "ClOvEr", "cHeRry"};

var sortedWords = words.OrderBy(a => a, new CaseInsensitiveComparer());

ObjectDumper.Write(sortedWords);

}

?OrderByDescending - Simple 1

public void Linq32() {

double[] doubles = { 1.7, 2.3, 1.9, 4.1, 2.9 };

var sortedDoubles =

from d in doubles

orderby d descending

select d;

Console.WriteLine("The doubles from highest to lowest:");

foreach (var d in sortedDoubles) {

Console.WriteLine(d);

}

}

?OrderByDescending - Simple 2

public void Linq33() {

List products = GetProductList();

相关文档