forked from rogerwcpt/python-linq-samples
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathProgram.cs
77 lines (60 loc) · 2.62 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
using System;
using System.Linq;
using System.ComponentModel;
using linqshared;
namespace linq_element
{
class Program : ProgramBase
{
static void Main(string[] args)
{
Linq58();
// Linq59();
// Linq61();
// Linq62();
// Linq64();
}
[Category("Element Operators")]
[Description("This sample returns the first matching element as a Product, instead of as a sequence containing a Product.")]
static void Linq58()
{
var products = GetProductList();
var product12 = products.First(p => p.ProductID == 12);
ObjectDumper.Write(product12);
}
[Category("Element Operators")]
[Description("This sample finds the first element in the array that starts with 'o'.")]
static void Linq59()
{
var strings = new []{ "zero", "one", "two", "three", "four", "five", "six", "seven", "eight", "nine" };
var startsWithO = strings.First(s => s.StartsWith('o'));
Console.WriteLine("A string starting with 'o': {0}", startsWithO);
}
[Category("Element Operators")]
[Description("This sample returns the first or default if nothing is found, to try to return the first element of the sequence unless there are no elements, in which case the default value for that type is returned.")]
static void Linq61()
{
var numbers = new int[0];
var firstNumOrDefault = numbers.FirstOrDefault();
Console.WriteLine(firstNumOrDefault);
}
[Category("Element Operators")]
[Description("This sample returns the first or default if nothing is found, to return the first product whose ProductID is 789 as a single Product object, unless there is no match, in which case null is returned.")]
static void Linq62()
{
var products = GetProductList();
var product789 = products.FirstOrDefault(p => p.ProductID == 789);
Console.WriteLine("Product 789 exists: {0}", product789 != null);
}
[Category("Element Operators")]
[Description("This sample retrieve the second number greater than 5 from an array.")]
static void Linq64()
{
var numbers = new [] { 5, 4, 1, 3, 9, 8, 6, 7, 2, 0 };
var fourthLowNum = numbers
.Where(num => num > 5)
.ElementAt(1);
Console.WriteLine("Second number > 5: {0}", fourthLowNum);
}
}
}