Linq101-Projection

时间:2021-03-25 20:03:27
 using System;
using System.Linq; namespace Linq101
{
class Projection
{
/// <summary>
/// This sample uses select to produce a sequence of ints one higher than those in an existing array of ints.
/// </summary>
public void Linq6()
{
int[] numbers = { , , , , , , , , , }; var query = from n in numbers
select n + ; Console.WriteLine("Numbers + 1 :");
foreach (var i in query)
{
Console.WriteLine(i);
}
} /// <summary>
/// This sample uses select to return a sequence of just the names of a list of products.
/// </summary>
public void Linq7()
{
var products = Data.GetProductList(); var query = from p in products
select p.ProductName; Console.WriteLine("Product Names:");
foreach (var productName in query)
{
Console.WriteLine(productName);
}
} /// <summary>
/// This sample uses select to produce a sequence of strings representing the text version of a sequence of ints.
/// </summary>
public void Linq8()
{
int[] numbers = { , , , , , , , , , };
string[] strings = { "zero", "one", "two", "three", "four", "five", "six", "seven", "eight", "nine" }; var query = from n in numbers
select strings[n]; Console.WriteLine("Number strings:");
foreach (var s in query)
{
Console.WriteLine(s);
}
} /// <summary>
/// This sample uses select to produce a sequence of the uppercase and lowercase versions of each word in the original array.
/// </summary>
public void Linq9()
{
string[] words = { "aPPLE", "BlUeBeRrY", "cHeRry" }; var query = from w in words
select new { U = w.ToUpper(), L = w.ToLower() }; Console.WriteLine("Results:");
foreach (var item in query)
{
Console.WriteLine("Uppercase:{0},Lowercase:{1}", item.U, item.L);
}
} /// <summary>
/// This sample uses select to produce a sequence containing text representations of digits and whether their length? is even or odd.
/// </summary>
public void Linq10()
{
int[] numbers = { , , , , , , , , , };
string[] strings = { "zero", "one", "two", "three", "four", "five", "six", "seven", "eight", "nine" }; var query = from n in numbers
select new { Digit = strings[n], EorO = n % == ? "Even" : "Odd" }; Console.WriteLine("Results:");
foreach (var item in query)
{
Console.WriteLine("The digit {0} is {1}", item.Digit, item.EorO);
}
} /// <summary>
/// This sample uses select to produce a sequence containing some properties of Products, including UnitPrice which is renamed to Price in the resulting type.
/// </summary>
public void Linq11()
{
var products = Data.GetProductList(); var query = from p in products
select new { p.ProductName, p.Category, Price = p.UnitPrice }; Console.WriteLine("Product Info:");
foreach (var product in query)
{
Console.WriteLine("{0} is in the category {1} and cost {2} per unit", product.ProductName, product.Category, product.Price);
}
} /// <summary>
/// This sample uses an indexed Select clause to determine if the value of ints in an array match their position in the array.
/// </summary>
public void Linq12()
{
int[] numbers = { , , , , , , , , , }; var query = numbers.Select((n, index) => new { Num = n, InPlace = n == index }); Console.WriteLine("Number:In-place?");
foreach (var number in query)
{
Console.WriteLine("{0}:{1}", number.Num, number.InPlace);
}
} /// <summary>
/// This sample combines select and where to make a simple query that returns the text form of each digit less than 5.
/// </summary>
public void Linq13()
{
int[] numbers = { , , , , , , , , , };
string[] digits = { "zero", "one", "two", "three", "four", "five", "six", "seven", "eight", "nine" }; var query = from n in numbers
where n <
select digits[n]; Console.WriteLine("Numbers < 5:");
foreach (var digit in query)
{
Console.WriteLine(digit);
}
} /// <summary>
/// This sample uses a compound from clause to make a query that returns all pairs of numbers from both arrays such that the number from numbersA is less than the number from numbersB.
/// </summary>
public void Linq14()
{
int[] numbersA = { , , , , , , };
int[] numbersB = { , , , , }; var query = from a in numbersA
from b in numbersB
where a < b
select new { a, b }; Console.WriteLine("Pairs where a < b");
foreach (var pair in query)
{
Console.WriteLine("{0} is less than {1}", pair.a, pair.b);
}
} /// <summary>
/// This sample uses a compound from clause to select all orders where the order total is less than 500.00.
/// </summary>
public void Linq15()
{
var customers = Data.GetCustomerList(); var query = from c in customers
from o in c.Orders
where o.Total <
select new { c.CustomerID, o.OrderID, o.Total }; ObjectDumper.Write(query);
} /// <summary>
/// This sample uses a compound from clause to select all orders where the order was made in 1998 or later.
/// </summary>
public void Linq16()
{
var customers = Data.GetCustomerList(); var query = from c in customers
from o in c.Orders
where o.OrderDate >= new DateTime(, , )
select new { c.CustomerID, o.OrderID, o.OrderDate }; ObjectDumper.Write(query);
} /// <summary>
/// This sample uses a compound from clause to select all orders where the order total is greater than 2000.00 and uses from assignment to avoid requesting the total twice.
/// </summary>
public void Linq17()
{
var customers = Data.GetCustomerList(); var query = from c in customers
from o in c.Orders
where o.Total >
select new { c.CustomerID, o.OrderID, o.Total }; ObjectDumper.Write(query);
} /// <summary>
/// This sample uses multiple from clauses so that filtering on customers can be done before selecting their orders. This makes the query more efficient by not selecting and then discarding orders for customers outside of Washington.
/// </summary>
public void Linq18()
{
var customers = Data.GetCustomerList(); //效率低
//var query = from c in customers
// from o in c.Orders
// where c.Region == "WA" && o.OrderDate >= new DateTime(1997, 1, 1)
// select new { c.CustomerID, o.OrderID }; var query = from c in customers
where c.Region == "WA"
from o in c.Orders
where o.OrderDate >= new DateTime(, , )
select new { c.CustomerID, o.OrderID }; ObjectDumper.Write(query);
} /// <summary>
/// This sample uses an indexed SelectMany clause to select all orders, while referring to customers by the order in which they are returned from the query.
/// </summary>
public void Linq19()
{
var customers = Data.GetCustomerList(); //var query = customers.SelectMany(c => c.Orders);
//var query = customers.SelectMany(c => c.Orders).Where(o => o.OrderDate >= new DateTime(1998, 1, 1));
var query =
customers.SelectMany(
(customer, index) =>
(customer.Orders.Select(o => "Customer #" + (index + ) + " has an order with OrderID " + o.OrderID))); ObjectDumper.Write(query);
}
}
}

相关文章