diff --git a/src/Chapter15.Tests/Listing15.03.AnonymousTypesWithinQueryExpressions.Tests.cs b/src/Chapter15.Tests/Listing15.03.TuplesWithinQueryExpressions.Tests.cs similarity index 100% rename from src/Chapter15.Tests/Listing15.03.AnonymousTypesWithinQueryExpressions.Tests.cs rename to src/Chapter15.Tests/Listing15.03.TuplesWithinQueryExpressions.Tests.cs diff --git a/src/Chapter15.Tests/Listing15.03A.AnonymousTypesWithinQueryExpressions.Tests.cs b/src/Chapter15.Tests/Listing15.03A.AnonymousTypesWithinQueryExpressions.Tests.cs new file mode 100644 index 000000000..85076c4e0 --- /dev/null +++ b/src/Chapter15.Tests/Listing15.03A.AnonymousTypesWithinQueryExpressions.Tests.cs @@ -0,0 +1,31 @@ +using IntelliTect.TestTools.Console; +using Microsoft.VisualStudio.TestTools.UnitTesting; +using System.Collections.Generic; +using System.IO; +using System.Linq; + +namespace AddisonWesley.Michaelis.EssentialCSharp.Chapter15.Listing15_03.Tests +{ + [TestClass] + public class ProgramTests + { + [TestMethod] + public void ProjectionWithLinqsSelect() + { + string expectedPattern = $@"{ Directory.GetCurrentDirectory() }{Path.DirectorySeparatorChar}*(*)"; + + string output = IntelliTect.TestTools.Console.ConsoleAssert.Execute(null, () => + { + Program.ChapterMain(); + }); + + IEnumerable outputItems = output.Split('\n'); + + Assert.AreEqual(14, outputItems.Count()); + foreach (string item in outputItems) + { + Assert.IsTrue(item.IsLike(expectedPattern)); + } + } + } +} diff --git a/src/Chapter15/Chapter15.csproj b/src/Chapter15/Chapter15.csproj index f7a0a8f04..09819ac08 100644 Binary files a/src/Chapter15/Chapter15.csproj and b/src/Chapter15/Chapter15.csproj differ diff --git a/src/Chapter15/Listing15.03.TuplesWithinQueryExpressions.cs b/src/Chapter15/Listing15.03.TuplesWithinQueryExpressions.cs new file mode 100644 index 000000000..a53de0ef3 --- /dev/null +++ b/src/Chapter15/Listing15.03.TuplesWithinQueryExpressions.cs @@ -0,0 +1,35 @@ +namespace AddisonWesley.Michaelis.EssentialCSharp.Chapter15.Listing15_03 +{ + using System; + using System.Collections.Generic; + using System.IO; + using System.Linq; + + public class Program + { + public static void ChapterMain() + { + List2(Directory.GetCurrentDirectory(), "*"); + } + + static void List2(string rootDirectory, string searchPattern) + { + var fileNames = Directory.EnumerateFiles( + rootDirectory, searchPattern); + var fileResults = + from fileName in fileNames + select + ( + Name: fileName, + LastWriteTime: File.GetLastWriteTime(fileName) + ); + + foreach (var fileResult in fileResults) + { + Console.WriteLine( + $@"{ fileResult.Name } ({ + fileResult.LastWriteTime })"); + } + } + } +} \ No newline at end of file diff --git a/src/Chapter15/Listing15.03.AnonymousTypesWithinQueryExpressions.cs b/src/Chapter15/Listing15.03A.AnonymousTypesWithinQueryExpressions.cs similarity index 98% rename from src/Chapter15/Listing15.03.AnonymousTypesWithinQueryExpressions.cs rename to src/Chapter15/Listing15.03A.AnonymousTypesWithinQueryExpressions.cs index 761c24669..d7d1ddd1a 100644 --- a/src/Chapter15/Listing15.03.AnonymousTypesWithinQueryExpressions.cs +++ b/src/Chapter15/Listing15.03A.AnonymousTypesWithinQueryExpressions.cs @@ -1,4 +1,4 @@ -namespace AddisonWesley.Michaelis.EssentialCSharp.Chapter15.Listing15_03 +namespace AddisonWesley.Michaelis.EssentialCSharp.Chapter15.Listing15_03A { using System; using System.Collections.Generic; diff --git a/src/Chapter15/Listing15.11.SelectingAnAnonymousTypeFollowingTheGroupbyClause.cs b/src/Chapter15/Listing15.11.SelectingTupleFollowingTheGroupbyClause.cs similarity index 82% rename from src/Chapter15/Listing15.11.SelectingAnAnonymousTypeFollowingTheGroupbyClause.cs rename to src/Chapter15/Listing15.11.SelectingTupleFollowingTheGroupbyClause.cs index 527f00b39..de54a5f4c 100644 --- a/src/Chapter15/Listing15.11.SelectingAnAnonymousTypeFollowingTheGroupbyClause.cs +++ b/src/Chapter15/Listing15.11.SelectingTupleFollowingTheGroupbyClause.cs @@ -17,21 +17,25 @@ private static void GroupKeywords1() from word in Keywords group word by word.Contains('*'); - var selection = + IEnumerable<(bool IsContextualKeyword, IGrouping Items)> selection = from groups in keywordGroups - select new - { - IsContextualKeyword = groups.Key, - Items = groups - }; + select + ( + IsContextualKeyword: groups.Key, + Items: groups + ); + - foreach(var wordGroup in selection) + foreach ( + (bool IsContextualKeyword, IGrouping Items) + wordGroup in selection) { + Console.WriteLine(Environment.NewLine + "{0}:", wordGroup.IsContextualKeyword ? "Contextual Keywords" : "Keywords"); - foreach(var keyword in wordGroup.Items) + foreach (string keyword in wordGroup.Items) { Console.Write(" " + keyword.Replace("*", null)); } diff --git a/src/Chapter15/Listing15.11A.SelectingAnAnonymousTypeFollowingTheGroupbyClause.cs b/src/Chapter15/Listing15.11A.SelectingAnAnonymousTypeFollowingTheGroupbyClause.cs new file mode 100644 index 000000000..f2530f079 --- /dev/null +++ b/src/Chapter15/Listing15.11A.SelectingAnAnonymousTypeFollowingTheGroupbyClause.cs @@ -0,0 +1,67 @@ +namespace AddisonWesley.Michaelis.EssentialCSharp.Chapter15.Listing15_11A +{ + using System; + using System.Collections.Generic; + using System.Linq; + + public class Program + { + public static void ChapterMain() + { + GroupKeywords1(); + } + + private static void GroupKeywords1() + { + IEnumerable> keywordGroups = + from word in Keywords + group word by word.Contains('*'); + + IEnumerable<(bool IsContextualKeyword, IGrouping Items)> selection = + from groups in keywordGroups + select + ( + IsContextualKeyword: groups.Key, + Items: groups + ); + + + foreach ( + (bool IsContextualKeyword, IGrouping Items) + wordGroup in selection) + { + + Console.WriteLine(Environment.NewLine + "{0}:", + wordGroup.IsContextualKeyword ? + "Contextual Keywords" : "Keywords"); + + foreach (string keyword in wordGroup.Items) + { + Console.Write(" " + keyword.Replace("*", null)); + } + } + } + + private static string[] Keywords = { + "abstract", "add*", "alias*", "as", "ascending*", + "async*", "await*", "base","bool", "break", + "by*", "byte", "case", "catch", "char", "checked", + "class", "const", "continue", "decimal", "default", + "delegate", "descending*", "do", "double", + "dynamic*", "else", "enum", "event", "equals*", + "explicit", "extern", "false", "finally", "fixed", + "from*", "float", "for", "foreach", "get*", "global*", + "group*", "goto", "if", "implicit", "in", "int", + "into*", "interface", "internal", "is", "lock", "long", + "join*", "let*", "nameof*", "namespace", "new", "null", + "on*", "operator", "orderby*", "out", "override", + "object", "params", "partial*", "private", "protected", + "public", "readonly", "ref", "remove*", "return", "sbyte", + "sealed", "select*", "set*", "short", "sizeof", + "stackalloc", "static", "string", "struct", "switch", + "this", "throw", "true", "try", "typeof", "uint", "ulong", + "unsafe", "ushort", "using", "value*", "var*", "virtual", + "unchecked", "void", "volatile", "where*", "while", "yield*" + }; + } +} diff --git a/src/Chapter15/Listing15.12.SelectingWithoutTheQueryContinuation.cs b/src/Chapter15/Listing15.12.SelectingWithoutTheQueryContinuation.cs index 50a558a27..8d0f595b7 100644 --- a/src/Chapter15/Listing15.12.SelectingWithoutTheQueryContinuation.cs +++ b/src/Chapter15/Listing15.12.SelectingWithoutTheQueryContinuation.cs @@ -1,6 +1,7 @@ -namespace AddisonWesley.Michaelis.EssentialCSharp.Chapter15.Listing15_12 +namespace AddisonWesley.Michaelis.EssentialCSharp.Chapter15.Listing15_12A { using System; + using System.Collections.Generic; using System.Linq; public class Program @@ -12,17 +13,18 @@ public static void ChapterMain() private static void GroupKeywords1() { - var selection = + IEnumerable<(bool IsContextualKeyword, IGrouping Items)> selection = from word in Keywords group word by word.Contains('*') into groups - select new - { - IsContextualKeyword = groups.Key, - Items = groups - }; + select + ( + IsContextualKeyword: groups.Key, + Items: groups + ); - foreach(var wordGroup in selection) + + foreach (var wordGroup in selection) { Console.WriteLine(Environment.NewLine + "{0}:", wordGroup.IsContextualKeyword ? diff --git a/src/Chapter15/Listing15.12A.SelectingAnonymousTypeWithoutTheQueryContinuation.cs b/src/Chapter15/Listing15.12A.SelectingAnonymousTypeWithoutTheQueryContinuation.cs new file mode 100644 index 000000000..50a558a27 --- /dev/null +++ b/src/Chapter15/Listing15.12A.SelectingAnonymousTypeWithoutTheQueryContinuation.cs @@ -0,0 +1,60 @@ +namespace AddisonWesley.Michaelis.EssentialCSharp.Chapter15.Listing15_12 +{ + using System; + using System.Linq; + + public class Program + { + public static void ChapterMain() + { + GroupKeywords1(); + } + + private static void GroupKeywords1() + { + var selection = + from word in Keywords + group word by word.Contains('*') + into groups + select new + { + IsContextualKeyword = groups.Key, + Items = groups + }; + + foreach(var wordGroup in selection) + { + Console.WriteLine(Environment.NewLine + "{0}:", + wordGroup.IsContextualKeyword ? + "Contextual Keywords" : "Keywords"); + foreach(var keyword in wordGroup.Items) + { + Console.Write(" " + + keyword.Replace("*", null)); + } + } + } + + private static string[] Keywords = { + "abstract", "add*", "alias*", "as", "ascending*", + "async*", "await*", "base","bool", "break", + "by*", "byte", "case", "catch", "char", "checked", + "class", "const", "continue", "decimal", "default", + "delegate", "descending*", "do", "double", + "dynamic*", "else", "enum", "event", "equals*", + "explicit", "extern", "false", "finally", "fixed", + "from*", "float", "for", "foreach", "get*", "global*", + "group*", "goto", "if", "implicit", "in", "int", + "into*", "interface", "internal", "is", "lock", "long", + "join*", "let*", "nameof*", "namespace", "new", "null", + "on*", "operator", "orderby*", "out", "override", + "object", "params", "partial*", "private", "protected", + "public", "readonly", "ref", "remove*", "return", "sbyte", + "sealed", "select*", "set*", "short", "sizeof", + "stackalloc", "static", "string", "struct", "switch", + "this", "throw", "true", "try", "typeof", "uint", "ulong", + "unsafe", "ushort", "using", "value*", "var*", "virtual", + "unchecked", "void", "volatile", "where*", "while", "yield*" + }; + } +} diff --git a/src/Chapter15/Listing15.14.CartesianProduct.cs b/src/Chapter15/Listing15.14.CartesianProduct.cs index 22c88dd7b..c5b084b65 100644 --- a/src/Chapter15/Listing15.14.CartesianProduct.cs +++ b/src/Chapter15/Listing15.14.CartesianProduct.cs @@ -1,6 +1,7 @@ namespace AddisonWesley.Michaelis.EssentialCSharp.Chapter15.Listing15_14 { using System; + using System.Collections.Generic; using System.Linq; public class Program @@ -13,15 +14,15 @@ public static void ChapterMain() private static void KeywordProducts() { var numbers = new[] { 1, 2, 3 }; - var product = + IEnumerable<(string Word, int Number)> product = from word in Keywords from number in numbers - select new { word, number }; + select (word, number); - foreach(var value in product) + foreach ((string Word, int Number) value in product) { - Console.WriteLine(Environment.NewLine + - $"({value.word}, {value.number})"); + Console.WriteLine( + $"({value.Word}, {value.Number})"); } }