Created
June 22, 2010 13:50
-
-
Save forki/448477 to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
public class EnumerableLookupSample | |
{ | |
private static readonly Dictionary<string, string> FullNamesDb = | |
new Dictionary<string, string> | |
{ | |
{ "Bill Gates", "billg@microsoft.com" }, | |
{ "Bill Clinton", "bill@hope.ar.us" }, | |
{ "Michael Jackson", "mj@wonderland.org" } | |
}; | |
private static readonly Dictionary<string, string> NickNamesDb = | |
new Dictionary<string, string> | |
{ | |
{ "billy", "billg@microsoft.com" }, | |
{ "slick willy", "bill@hope.ar.us" }, | |
{ "jacko", "mj@wonderland.org" } | |
}; | |
private static readonly Dictionary<string, string> PrefsDb = | |
new Dictionary<string, string> | |
{ | |
{ "billg@microsoft.com", "HTML" }, | |
{ "bill@hope.ar.us", "Plain" }, | |
{ "mj@wonderland.org", "HTML" } | |
}; | |
[Fact] | |
public void DictionaryLookup() | |
{ | |
Assert.Equal("HTML", LookUp("billy").First()); | |
Assert.Equal("HTML", LookUp("Bill Gates").First()); | |
Assert.Equal(0, LookUp("Steffen").Count()); | |
} | |
private static IEnumerable<string> LookUp(string text) | |
{ | |
return | |
FullNamesDb.TryFind(text) | |
.Concat(NickNamesDb.TryFind(text)) | |
.SelectMany(y => PrefsDb.TryFind(y)); | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment