Created
June 22, 2010 13:50
-
-
Save forki/448477 to your computer and use it in GitHub Desktop.
This file contains hidden or 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", "[email protected]" }, | |
{ "Bill Clinton", "[email protected]" }, | |
{ "Michael Jackson", "[email protected]" } | |
}; | |
private static readonly Dictionary<string, string> NickNamesDb = | |
new Dictionary<string, string> | |
{ | |
{ "billy", "[email protected]" }, | |
{ "slick willy", "[email protected]" }, | |
{ "jacko", "[email protected]" } | |
}; | |
private static readonly Dictionary<string, string> PrefsDb = | |
new Dictionary<string, string> | |
{ | |
{ "[email protected]", "HTML" }, | |
{ "[email protected]", "Plain" }, | |
{ "[email protected]", "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