Guest User

Untitled

a guest
May 29th, 2018
248
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 1.49 KB | None | 0 0
  1. public class EnumerableLookupSample
  2. {
  3. private static readonly Dictionary<string, string> FullNamesDb =
  4. new Dictionary<string, string>
  5. {
  6. { "Bill Gates", "billg@microsoft.com" },
  7. { "Bill Clinton", "bill@hope.ar.us" },
  8. { "Michael Jackson", "mj@wonderland.org" }
  9. };
  10.  
  11. private static readonly Dictionary<string, string> NickNamesDb =
  12. new Dictionary<string, string>
  13. {
  14. { "billy", "billg@microsoft.com" },
  15. { "slick willy", "bill@hope.ar.us" },
  16. { "jacko", "mj@wonderland.org" }
  17. };
  18.  
  19. private static readonly Dictionary<string, string> PrefsDb =
  20. new Dictionary<string, string>
  21. {
  22. { "billg@microsoft.com", "HTML" },
  23. { "bill@hope.ar.us", "Plain" },
  24. { "mj@wonderland.org", "HTML" }
  25. };
  26.  
  27. [Fact]
  28. public void DictionaryLookup()
  29. {
  30. Assert.Equal("HTML", LookUp("billy").First());
  31. Assert.Equal("HTML", LookUp("Bill Gates").First());
  32. Assert.Equal(0, LookUp("Steffen").Count());
  33. }
  34.  
  35. private static IEnumerable<string> LookUp(string text)
  36. {
  37. return
  38. FullNamesDb.TryFind(text)
  39. .Concat(NickNamesDb.TryFind(text))
  40. .SelectMany(y => PrefsDb.TryFind(y));
  41.  
  42. }
  43. }
Add Comment
Please, Sign In to add comment