-
Notifications
You must be signed in to change notification settings - Fork 212
/
Copy pathLinqExecutor.cs
65 lines (54 loc) · 2.25 KB
/
LinqExecutor.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
using BuildingBlocks;
namespace FluentInterfaceLibrary.LinqExample;
public static class LinqExecutor
{
public static void Execute()
{
ConsoleExtension.WriteSeparator("LINQ example");
var englishToSerbianDictionary = new Dictionary<string, string>
{
{"adventure", "avantura"},
{"bird", "ptica"},
{"fish", "riba"},
{"football", "fudbal"},
{"programming", "programiranje"},
};
DisplayDictionary(englishToSerbianDictionary);
Console.WriteLine("\nFinding translations for English words containing the letter 'a', sorted by length and displayed in uppercase...");
FindTranslationsProgressively(englishToSerbianDictionary);
FindTranslationsUsingFluentInterface(englishToSerbianDictionary);
}
private static void DisplayDictionary(Dictionary<string, string> englishToSerbianDictionary)
{
Console.WriteLine("\nContent of the dictionary: ");
foreach (var (englishWord, serbianWord) in englishToSerbianDictionary)
{
Console.WriteLine($"{englishWord} - {serbianWord}");
}
}
private static void FindTranslationsProgressively(Dictionary<string, string> englishToSerbianDictionary)
{
var filtered = englishToSerbianDictionary.Where(t => t.Key.Contains("a"));
var sorted = filtered.OrderBy(t => t.Value.Length);
var finalTranslations = sorted.Select(t => t.Value.ToUpper());
Console.WriteLine("\nProgressive translations: ");
DisplayWords(finalTranslations);
}
private static void FindTranslationsUsingFluentInterface(Dictionary<string, string> englishToSerbianDictionary)
{
// C# uses fluent programming extensively in LINQ to build queries using "standard query operators".
var finalTranslations = englishToSerbianDictionary
.Where(t => t.Key.Contains("a"))
.OrderBy(t => t.Value.Length)
.Select(t => t.Value.ToUpper());
Console.WriteLine("\nFluent interface translations: ");
DisplayWords(finalTranslations);
}
private static void DisplayWords(IEnumerable<string> words)
{
foreach (var word in words)
{
Console.WriteLine(word);
}
}
}