Skip to content

Commit

Permalink
Moved anonymous type samples to alternates and created tuple implemen…
Browse files Browse the repository at this point in the history
…tations.
  • Loading branch information
MarkMichaelis committed Jul 14, 2017
1 parent b4c62d8 commit 500d7ac
Show file tree
Hide file tree
Showing 10 changed files with 222 additions and 22 deletions.
Original file line number Diff line number Diff line change
@@ -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<string> outputItems = output.Split('\n');

Assert.AreEqual(14, outputItems.Count());
foreach (string item in outputItems)
{
Assert.IsTrue(item.IsLike(expectedPattern));
}
}
}
}
Binary file modified src/Chapter15/Chapter15.csproj
Binary file not shown.
35 changes: 35 additions & 0 deletions src/Chapter15/Listing15.03.TuplesWithinQueryExpressions.cs
Original file line number Diff line number Diff line change
@@ -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 })");
}
}
}
}
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
namespace AddisonWesley.Michaelis.EssentialCSharp.Chapter15.Listing15_03
namespace AddisonWesley.Michaelis.EssentialCSharp.Chapter15.Listing15_03A
{
using System;
using System.Collections.Generic;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -17,21 +17,25 @@ private static void GroupKeywords1()
from word in Keywords
group word by word.Contains('*');

var selection =
IEnumerable<(bool IsContextualKeyword, IGrouping<bool, string> 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<bool, string> 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));
}
Expand Down
Original file line number Diff line number Diff line change
@@ -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<IGrouping<bool, string>> keywordGroups =
from word in Keywords
group word by word.Contains('*');

IEnumerable<(bool IsContextualKeyword, IGrouping<bool, string> Items)> selection =
from groups in keywordGroups
select
(
IsContextualKeyword: groups.Key,
Items: groups
);


foreach (
(bool IsContextualKeyword, IGrouping<bool, string> 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*"
};
}
}
18 changes: 10 additions & 8 deletions src/Chapter15/Listing15.12.SelectingWithoutTheQueryContinuation.cs
Original file line number Diff line number Diff line change
@@ -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
Expand All @@ -12,17 +13,18 @@ public static void ChapterMain()

private static void GroupKeywords1()
{
var selection =
IEnumerable<(bool IsContextualKeyword, IGrouping<bool, string> 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 ?
Expand Down
Original file line number Diff line number Diff line change
@@ -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*"
};
}
}
11 changes: 6 additions & 5 deletions src/Chapter15/Listing15.14.CartesianProduct.cs
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
namespace AddisonWesley.Michaelis.EssentialCSharp.Chapter15.Listing15_14
{
using System;
using System.Collections.Generic;
using System.Linq;

public class Program
Expand All @@ -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})");
}
}

Expand Down

0 comments on commit 500d7ac

Please sign in to comment.