forked from rogerwcpt/python-linq-samples
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathProgram.cs
86 lines (69 loc) · 3.08 KB
/
Program.cs
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
using System;
using System.Linq;
using linqshared;
using System.ComponentModel;
namespace linq_restrictions
{
class Program: ProgramBase
{
static void Main(string[] args)
{
Linq1();
// Linq2();
// Linq3();
// Linq4();
// Linq5();
}
[Category("Restriction Operators")]
[Description("This sample uses a filter to find all elements of an array with a value less than 5.")]
static void Linq1()
{
var numbers = new [] { 5, 4, 1, 3, 9, 8, 6, 7, 2, 0 };
var lowNums = numbers.Where(n => n < 5);
Console.WriteLine("Numbers < 5:");
lowNums.ForEach(Console.WriteLine);
}
[Category("Restriction Operators")]
[Description("This sample uses a filter to find all products that are out of stock.")]
static void Linq2()
{
var products = GetProductList();
var soldOutProducts = products.Where(p => p.UnitsInStock == 0);
Console.WriteLine("Sold out products:");
soldOutProducts.ForEach(x => Console.WriteLine($"{x.ProductName} is sold out!"));
}
[Category("Restriction Operators")]
[Description("This sample uses a filter to find all products that are in stock and cost more than 3.00 per unit.")]
public static void Linq3()
{
var products = GetProductList();
var expensiveInStockProducts = products.Where(p => p.UnitsInStock > 0 && p.UnitPrice > 3.00M);
Console.WriteLine("In-stock products that cost more than 3.00:");
expensiveInStockProducts.ForEach(product => Console.WriteLine($"{product.ProductName} is in stock and costs more than 3.00."));
}
[Category("Restriction Operators")]
[Description("This sample uses a filter to find all customers in Washington and then it uses a foreach loop to iterate over the orders collection that belongs to each customer.")]
static void Linq4()
{
var customers = GetCustomerList();
Console.WriteLine("Customers from Washington and their orders:");
var waCustomers = customers.Where(c => c.Region == "WA");
waCustomers.ForEach((customer) =>
{
Console.WriteLine($"Customer {customer.CustomerID}: {customer.CompanyName}");
customer.Orders.ForEach((order) =>
{
Console.WriteLine($" Order {order.OrderID}: {order.OrderDate}");
});
});
}
[Description("This sample demonstrates an indexed filter that returns digits whose name is shorter than their value.")]
static void Linq5()
{
var digits = new[] { "zero", "one", "two", "three", "four", "five", "six", "seven", "eight", "nine" };
var shortDigits = digits.Where((digit, index) => digit.Length < index);
Console.WriteLine("Short digits:");
shortDigits.ForEach(d => Console.WriteLine($"The word {d} is shorter than its value."));
}
}
}