Skip to content

Commit 3fb85fc

Browse files
committed
Merge pull request dotnet#108 from matthewreily/linq-samples-for-restriction
Linq samples for restriction
2 parents aba8555 + 674ff39 commit 3fb85fc

File tree

7 files changed

+258
-5
lines changed

7 files changed

+258
-5
lines changed
+89
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,89 @@
1+
using System.Collections.Generic;
2+
3+
namespace Restriction
4+
{
5+
//The Data class is used as a fake repository
6+
public class Data
7+
{
8+
public static List<Product> Products { get; } = new List<Product>()
9+
{
10+
new Product { ProductID = 1, ProductName = "Chai", Category = "Beverages", UnitPrice = 18.0000M, UnitsInStock = 39 },
11+
new Product { ProductID = 2, ProductName = "Chang", Category = "Beverages", UnitPrice = 19.0000M, UnitsInStock = 17 },
12+
new Product { ProductID = 3, ProductName = "Aniseed Syrup", Category = "Condiments", UnitPrice = 10.0000M, UnitsInStock = 13 },
13+
new Product { ProductID = 4, ProductName = "Chef Anton's Cajun Seasoning", Category = "Condiments", UnitPrice = 22.0000M, UnitsInStock = 53 },
14+
new Product { ProductID = 5, ProductName = "Chef Anton's Gumbo Mix", Category = "Condiments", UnitPrice = 21.3500M, UnitsInStock = 0 },
15+
new Product { ProductID = 6, ProductName = "Grandma's Boysenberry Spread", Category = "Condiments", UnitPrice = 25.0000M, UnitsInStock = 120 },
16+
new Product { ProductID = 7, ProductName = "Uncle Bob's Organic Dried Pears", Category = "Produce", UnitPrice = 30.0000M, UnitsInStock = 15 },
17+
new Product { ProductID = 8, ProductName = "Northwoods Cranberry Sauce", Category = "Condiments", UnitPrice = 40.0000M, UnitsInStock = 6 },
18+
new Product { ProductID = 9, ProductName = "Mishi Kobe Niku", Category = "Meat/Poultry", UnitPrice = 97.0000M, UnitsInStock = 29 },
19+
new Product { ProductID = 10, ProductName = "Ikura", Category = "Seafood", UnitPrice = 31.0000M, UnitsInStock = 31 },
20+
new Product { ProductID = 11, ProductName = "Queso Cabrales", Category = "Dairy Products", UnitPrice = 21.0000M, UnitsInStock = 22 },
21+
new Product { ProductID = 12, ProductName = "Queso Manchego La Pastora", Category = "Dairy Products", UnitPrice = 38.0000M, UnitsInStock = 86 },
22+
new Product { ProductID = 13, ProductName = "Konbu", Category = "Seafood", UnitPrice = 6.0000M, UnitsInStock = 24 },
23+
new Product { ProductID = 14, ProductName = "Tofu", Category = "Produce", UnitPrice = 23.2500M, UnitsInStock = 35 },
24+
new Product { ProductID = 15, ProductName = "Genen Shouyu", Category = "Condiments", UnitPrice = 15.5000M, UnitsInStock = 39 },
25+
new Product { ProductID = 16, ProductName = "Pavlova", Category = "Confections", UnitPrice = 17.4500M, UnitsInStock = 29 },
26+
new Product { ProductID = 17, ProductName = "Alice Mutton", Category = "Meat/Poultry", UnitPrice = 39.0000M, UnitsInStock = 0 },
27+
new Product { ProductID = 18, ProductName = "Carnarvon Tigers", Category = "Seafood", UnitPrice = 62.5000M, UnitsInStock = 42 },
28+
new Product { ProductID = 19, ProductName = "Teatime Chocolate Biscuits", Category = "Confections", UnitPrice = 9.2000M, UnitsInStock = 25 },
29+
new Product { ProductID = 20, ProductName = "Sir Rodney's Marmalade", Category = "Confections", UnitPrice = 81.0000M, UnitsInStock = 40 },
30+
new Product { ProductID = 21, ProductName = "Sir Rodney's Scones", Category = "Confections", UnitPrice = 10.0000M, UnitsInStock = 3 },
31+
new Product { ProductID = 22, ProductName = "Gustaf's Knäckebröd", Category = "Grains/Cereals", UnitPrice = 21.0000M, UnitsInStock = 104 },
32+
new Product { ProductID = 23, ProductName = "Tunnbröd", Category = "Grains/Cereals", UnitPrice = 9.0000M, UnitsInStock = 61 },
33+
new Product { ProductID = 24, ProductName = "Guaraná Fantástica", Category = "Beverages", UnitPrice = 4.5000M, UnitsInStock = 20 },
34+
new Product { ProductID = 25, ProductName = "NuNuCa Nuß-Nougat-Creme", Category = "Confections", UnitPrice = 14.0000M, UnitsInStock = 76 },
35+
new Product { ProductID = 26, ProductName = "Gumbär Gummibärchen", Category = "Confections", UnitPrice = 31.2300M, UnitsInStock = 15 },
36+
new Product { ProductID = 27, ProductName = "Schoggi Schokolade", Category = "Confections", UnitPrice = 43.9000M, UnitsInStock = 49 },
37+
new Product { ProductID = 28, ProductName = "Rössle Sauerkraut", Category = "Produce", UnitPrice = 45.6000M, UnitsInStock = 26 },
38+
new Product { ProductID = 29, ProductName = "Thüringer Rostbratwurst", Category = "Meat/Poultry", UnitPrice = 123.7900M, UnitsInStock = 0 },
39+
new Product { ProductID = 30, ProductName = "Nord-Ost Matjeshering", Category = "Seafood", UnitPrice = 25.8900M, UnitsInStock = 10 },
40+
new Product { ProductID = 31, ProductName = "Gorgonzola Telino", Category = "Dairy Products", UnitPrice = 12.5000M, UnitsInStock = 0 },
41+
new Product { ProductID = 32, ProductName = "Mascarpone Fabioli", Category = "Dairy Products", UnitPrice = 32.0000M, UnitsInStock = 9 },
42+
new Product { ProductID = 33, ProductName = "Geitost", Category = "Dairy Products", UnitPrice = 2.5000M, UnitsInStock = 112 },
43+
new Product { ProductID = 34, ProductName = "Sasquatch Ale", Category = "Beverages", UnitPrice = 14.0000M, UnitsInStock = 111 },
44+
new Product { ProductID = 35, ProductName = "Steeleye Stout", Category = "Beverages", UnitPrice = 18.0000M, UnitsInStock = 20 },
45+
new Product { ProductID = 36, ProductName = "Inlagd Sill", Category = "Seafood", UnitPrice = 19.0000M, UnitsInStock = 112 },
46+
new Product { ProductID = 37, ProductName = "Gravad lax", Category = "Seafood", UnitPrice = 26.0000M, UnitsInStock = 11 },
47+
new Product { ProductID = 38, ProductName = "Côte de Blaye", Category = "Beverages", UnitPrice = 263.5000M, UnitsInStock = 17 },
48+
new Product { ProductID = 39, ProductName = "Chartreuse verte", Category = "Beverages", UnitPrice = 18.0000M, UnitsInStock = 69 },
49+
new Product { ProductID = 40, ProductName = "Boston Crab Meat", Category = "Seafood", UnitPrice = 18.4000M, UnitsInStock = 123 },
50+
new Product { ProductID = 41, ProductName = "Jack's New England Clam Chowder", Category = "Seafood", UnitPrice = 9.6500M, UnitsInStock = 85 },
51+
new Product { ProductID = 42, ProductName = "Singaporean Hokkien Fried Mee", Category = "Grains/Cereals", UnitPrice = 14.0000M, UnitsInStock = 26 },
52+
new Product { ProductID = 43, ProductName = "Ipoh Coffee", Category = "Beverages", UnitPrice = 46.0000M, UnitsInStock = 17 },
53+
new Product { ProductID = 44, ProductName = "Gula Malacca", Category = "Condiments", UnitPrice = 19.4500M, UnitsInStock = 27 },
54+
new Product { ProductID = 45, ProductName = "Rogede sild", Category = "Seafood", UnitPrice = 9.5000M, UnitsInStock = 5 },
55+
new Product { ProductID = 46, ProductName = "Spegesild", Category = "Seafood", UnitPrice = 12.0000M, UnitsInStock = 95 },
56+
new Product { ProductID = 47, ProductName = "Zaanse koeken", Category = "Confections", UnitPrice = 9.5000M, UnitsInStock = 36 },
57+
new Product { ProductID = 48, ProductName = "Chocolade", Category = "Confections", UnitPrice = 12.7500M, UnitsInStock = 15 },
58+
new Product { ProductID = 49, ProductName = "Maxilaku", Category = "Confections", UnitPrice = 20.0000M, UnitsInStock = 10 },
59+
new Product { ProductID = 50, ProductName = "Valkoinen suklaa", Category = "Confections", UnitPrice = 16.2500M, UnitsInStock = 65 },
60+
new Product { ProductID = 51, ProductName = "Manjimup Dried Apples", Category = "Produce", UnitPrice = 53.0000M, UnitsInStock = 20 },
61+
new Product { ProductID = 52, ProductName = "Filo Mix", Category = "Grains/Cereals", UnitPrice = 7.0000M, UnitsInStock = 38 },
62+
new Product { ProductID = 53, ProductName = "Perth Pasties", Category = "Meat/Poultry", UnitPrice = 32.8000M, UnitsInStock = 0 },
63+
new Product { ProductID = 54, ProductName = "Tourtière", Category = "Meat/Poultry", UnitPrice = 7.4500M, UnitsInStock = 21 },
64+
new Product { ProductID = 55, ProductName = "Pâté chinois", Category = "Meat/Poultry", UnitPrice = 24.0000M, UnitsInStock = 115 },
65+
new Product { ProductID = 56, ProductName = "Gnocchi di nonna Alice", Category = "Grains/Cereals", UnitPrice = 38.0000M, UnitsInStock = 21 },
66+
new Product { ProductID = 57, ProductName = "Ravioli Angelo", Category = "Grains/Cereals", UnitPrice = 19.5000M, UnitsInStock = 36 },
67+
new Product { ProductID = 58, ProductName = "Escargots de Bourgogne", Category = "Seafood", UnitPrice = 13.2500M, UnitsInStock = 62 },
68+
new Product { ProductID = 59, ProductName = "Raclette Courdavault", Category = "Dairy Products", UnitPrice = 55.0000M, UnitsInStock = 79 },
69+
new Product { ProductID = 60, ProductName = "Camembert Pierrot", Category = "Dairy Products", UnitPrice = 34.0000M, UnitsInStock = 19 },
70+
new Product { ProductID = 61, ProductName = "Sirop d'érable", Category = "Condiments", UnitPrice = 28.5000M, UnitsInStock = 113 },
71+
new Product { ProductID = 62, ProductName = "Tarte au sucre", Category = "Confections", UnitPrice = 49.3000M, UnitsInStock = 17 },
72+
new Product { ProductID = 63, ProductName = "Vegie-spread", Category = "Condiments", UnitPrice = 43.9000M, UnitsInStock = 24 },
73+
new Product { ProductID = 64, ProductName = "Wimmers gute Semmelknödel", Category = "Grains/Cereals", UnitPrice = 33.2500M, UnitsInStock = 22 },
74+
new Product { ProductID = 65, ProductName = "Louisiana Fiery Hot Pepper Sauce", Category = "Condiments", UnitPrice = 21.0500M, UnitsInStock = 76 },
75+
new Product { ProductID = 66, ProductName = "Louisiana Hot Spiced Okra", Category = "Condiments", UnitPrice = 17.0000M, UnitsInStock = 4 },
76+
new Product { ProductID = 67, ProductName = "Laughing Lumberjack Lager", Category = "Beverages", UnitPrice = 14.0000M, UnitsInStock = 52 },
77+
new Product { ProductID = 68, ProductName = "Scottish Longbreads", Category = "Confections", UnitPrice = 12.5000M, UnitsInStock = 6 },
78+
new Product { ProductID = 69, ProductName = "Gudbrandsdalsost", Category = "Dairy Products", UnitPrice = 36.0000M, UnitsInStock = 26 },
79+
new Product { ProductID = 70, ProductName = "Outback Lager", Category = "Beverages", UnitPrice = 15.0000M, UnitsInStock = 15 },
80+
new Product { ProductID = 71, ProductName = "Flotemysost", Category = "Dairy Products", UnitPrice = 21.5000M, UnitsInStock = 26 },
81+
new Product { ProductID = 72, ProductName = "Mozzarella di Giovanni", Category = "Dairy Products", UnitPrice = 34.8000M, UnitsInStock = 14 },
82+
new Product { ProductID = 73, ProductName = "Röd Kaviar", Category = "Seafood", UnitPrice = 15.0000M, UnitsInStock = 101 },
83+
new Product { ProductID = 74, ProductName = "Longlife Tofu", Category = "Produce", UnitPrice = 10.0000M, UnitsInStock = 4 },
84+
new Product { ProductID = 75, ProductName = "Rhönbräu Klosterbier", Category = "Beverages", UnitPrice = 7.7500M, UnitsInStock = 125 },
85+
new Product { ProductID = 76, ProductName = "Lakkalikööri", Category = "Beverages", UnitPrice = 18.0000M, UnitsInStock = 57 },
86+
new Product { ProductID = 77, ProductName = "Original Frankfurter grüne Soße", Category = "Condiments", UnitPrice = 13.0000M, UnitsInStock = 32 }
87+
};
88+
}
89+
}
+11
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,11 @@
1+
namespace Restriction
2+
{
3+
public class Product
4+
{
5+
public int ProductID { get; set; }
6+
public string ProductName { get; set; }
7+
public string Category { get; set; }
8+
public decimal UnitPrice { get; set; }
9+
public int UnitsInStock { get; set; }
10+
}
11+
}

samples/linq/csharp/restriction/Program.cs

+9-3
Original file line numberDiff line numberDiff line change
@@ -2,11 +2,17 @@ namespace Restriction
22
{
33
public class Program
44
{
5+
//Entry point of application
56
public void Main(string[] args)
67
{
7-
var sample = new WhereClause1();
8-
sample.QuerySyntaxExample();
9-
sample.MethodSyntaxExample();
8+
//Invoke the linq restriction examples within the project
9+
WhereClause1.QuerySyntaxExample();
10+
WhereClause1.MethodSyntaxExample();
11+
WhereClause2.QuerySyntaxExample();
12+
WhereClause2.MethodSyntaxExample();
13+
WhereClause3.QuerySyntaxExample();
14+
WhereClause3.MethodSyntaxExample();
15+
WhereClause4.Example();
1016
}
1117
}
1218
}

samples/linq/csharp/restriction/Where-Sample-1.cs

+2-2
Original file line numberDiff line numberDiff line change
@@ -14,7 +14,7 @@ public class WhereClause1
1414
// 3
1515
// 2
1616
// 0
17-
public void QuerySyntaxExample()
17+
public static void QuerySyntaxExample()
1818
{
1919
int[] numbers = { 5, 4, 1, 3, 9, 8, 6, 7, 2, 0 };
2020

@@ -39,7 +39,7 @@ where n < 5
3939
// 3
4040
// 2
4141
// 0
42-
public void MethodSyntaxExample()
42+
public static void MethodSyntaxExample()
4343
{
4444
int[] numbers = { 5, 4, 1, 3, 9, 8, 6, 7, 2, 0 };
4545

Original file line numberDiff line numberDiff line change
@@ -0,0 +1,57 @@
1+
using System.Linq;
2+
using System;
3+
using System.Collections.Generic;
4+
5+
namespace Restriction
6+
{
7+
public class WhereClause2
8+
{
9+
//This sample uses the where clause to find all products that are out of stock using query syntax.
10+
//Outputs the following to Console
11+
//
12+
// Sold out products:
13+
// Chef Anton's Gumbo Mix is sold out!
14+
// Alice Mutton is sold out!
15+
// Thüringer Rostbratwurst s sold out!
16+
// Gorgonzola Telino is sold out!
17+
// Perth Pasties is sold out!
18+
public static void QuerySyntaxExample()
19+
{
20+
List<Product> products = Data.Products;
21+
22+
var soldOutProducts =
23+
from prod in products
24+
where prod.UnitsInStock == 0
25+
select prod;
26+
27+
Console.WriteLine("Sold out products:");
28+
foreach (var product in soldOutProducts)
29+
{
30+
Console.WriteLine("{0} is sold out!", product.ProductName);
31+
}
32+
}
33+
34+
//This sample uses the where clause to find all products that are out of stock using query syntax.
35+
//Outputs the following to Console
36+
//
37+
// Sold out products:
38+
// Chef Anton's Gumbo Mix is sold out!
39+
// Alice Mutton is sold out!
40+
// Thüringer Rostbratwurst s sold out!
41+
// Gorgonzola Telino is sold out!
42+
// Perth Pasties is sold out!
43+
public static void MethodSyntaxExample()
44+
{
45+
List<Product> products = Data.Products;
46+
47+
var soldOutProducts = products.Where(prod => prod.UnitsInStock == 0);
48+
49+
Console.WriteLine("Sold out products:");
50+
foreach (var product in soldOutProducts)
51+
{
52+
Console.WriteLine("{0} is sold out!", product.ProductName);
53+
}
54+
}
55+
56+
}
57+
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,60 @@
1+
using System.Linq;
2+
using System;
3+
using System.Collections.Generic;
4+
5+
namespace Restriction
6+
{
7+
public class WhereClause3
8+
{
9+
//This sample uses the where clause using query syntax to find all products that are in stock and cost
10+
//more than 50.00 per unit.
11+
//Outputs the following to Console
12+
//
13+
// In-stock products that cost more than 50.00:
14+
// Mishi Kobe Niku is in stock and costs more than 50.00.
15+
// Carnarvon Tigers is in stock and costs more than 50.00.
16+
// Sir Rodney's Marmalade is in stock and costs more than 50.00.
17+
// Côte de Blaye is in stock and costs more than 50.00.
18+
// Manjimup Dried Apples is in stock and costs more than 50.00.
19+
// Raclette Courdavault is in stock and costs more than 50.00.
20+
public static void QuerySyntaxExample()
21+
{
22+
List<Product> products = Data.Products;
23+
24+
var expensiveInStockProducts =
25+
from prod in products
26+
where prod.UnitsInStock > 0 && prod.UnitPrice > 50.00M
27+
select prod;
28+
29+
Console.WriteLine("In-stock products that cost more than 50.00:");
30+
foreach (var product in expensiveInStockProducts)
31+
{
32+
Console.WriteLine("{0} is in stock and costs more than 50.00.", product.ProductName);
33+
}
34+
}
35+
36+
//This sample uses the where clause using method syntax to find all products that are in stock and cost
37+
//more than 50.00 per unit.
38+
//Outputs the following to Console
39+
//
40+
// In-stock products that cost more than 50.00:
41+
// Mishi Kobe Niku is in stock and costs more than 50.00.
42+
// Carnarvon Tigers is in stock and costs more than 50.00.
43+
// Sir Rodney's Marmalade is in stock and costs more than 50.00.
44+
// Côte de Blaye is in stock and costs more than 50.00.
45+
// Manjimup Dried Apples is in stock and costs more than 50.00.
46+
// Raclette Courdavault is in stock and costs more than 50.00.
47+
public static void MethodSyntaxExample()
48+
{
49+
List<Product> products = Data.Products;
50+
51+
var expensiveInStockProducts = products.Where(prod=>prod.UnitsInStock > 0 && prod.UnitPrice > 50.00M);
52+
53+
Console.WriteLine("In-stock products that cost more than 50.00:");
54+
foreach (var product in expensiveInStockProducts)
55+
{
56+
Console.WriteLine("{0} is in stock and costs more than 50.00.", product.ProductName);
57+
}
58+
}
59+
}
60+
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,30 @@
1+
using System.Linq;
2+
using System;
3+
namespace Restriction
4+
{
5+
//This sample demonstrates an indexed where clause that returns digits whose name is
6+
//shorter than their value.
7+
//Outputs the following to Console
8+
//
9+
// Short digits:
10+
// The word five is shorter than its value.
11+
// The word six is shorter than its value.
12+
// The word seven is shorter than its value.
13+
// The word eight is shorter than its value.
14+
// The word nine is shorter than its value.
15+
public class WhereClause4
16+
{
17+
public static void Example()
18+
{
19+
string[] digits = { "zero", "one", "two", "three", "four", "five", "six", "seven", "eight", "nine" };
20+
21+
var shortDigits = digits.Where((digit, index) => digit.Length < index);
22+
23+
Console.WriteLine("Short digits:");
24+
foreach (var d in shortDigits)
25+
{
26+
Console.WriteLine("The word {0} is shorter than its value.", d);
27+
}
28+
}
29+
}
30+
}

0 commit comments

Comments
 (0)