Guest User

Untitled

a guest
Jul 6th, 2018
188
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 2.59 KB | None | 0 0
  1. void Main() {
  2. var logItems = GetLogItems();
  3.  
  4. var Messages = logItems.Select(item => new {
  5. From = extractFromEmailFromMessage(item.Message),
  6. SentToList = extractToEmailsFromMessage(item.Message)
  7. }).ToList();
  8.  
  9. var MessagesGroupByFrom = Messages
  10. .OrderBy(x => x.From)
  11. .GroupBy(x => x.From)
  12. .Select(x => new {
  13. From = x.Key,
  14. SentTo = x.SelectMany(y => y.SentToList)
  15. .GroupBy(y => y)
  16. .Select(g => new {
  17. To = g.Key,
  18. Count = g.Count()
  19. }).ToList()
  20. }).ToList();
  21.  
  22. MessagesGroupByFrom.ForEach(fromEmail => {
  23. Console.WriteLine($"--- {fromEmail.From} ---");
  24. fromEmail.SentTo.ForEach(toObj => {
  25. Console.WriteLine($" {toObj.To} Count: {toObj.Count} ");
  26. });
  27. });
  28.  
  29. }
  30.  
  31. class LogItem
  32. {
  33. public string Message;
  34. public string SessiongId;
  35. public string From;
  36. public string To;
  37. }
  38.  
  39. List<string> extractToEmailsFromMessage(string text) {
  40. var toPattern = @"to:\s+(\w+([-+.']\w+)*@\w+([-.]\w+)*\.\w+([-.]\w+)*)";
  41. var toRegex = new Regex(toPattern, RegexOptions.IgnoreCase);
  42.  
  43. var matches = new List<string>();
  44. foreach (Match match in toRegex.Matches(text)) {
  45. matches.Add(match.Groups[1].Value?.ToLower());
  46. }
  47.  
  48. return matches;
  49. }
  50.  
  51. string extractFromEmailFromMessage(string text) {
  52. var fromPattern = @"from:\s+(\w+([-+.']\w+)*@)\w+([-.]\w+)*\.\w+([-.]\w+)*";
  53. var fromRegex = new Regex(fromPattern, RegexOptions.IgnoreCase);
  54.  
  55. var match = fromRegex.Match(text);
  56. if (match.Success) {
  57. return match.Groups[1].Value.ToLower();
  58. }
  59. return "";
  60. }
  61.  
  62. List<LogItem> GetLogItems() {
  63. return
  64. new List<LogItem> {
  65. new LogItem() { Message = "from: rik@zoom.com to: paul@blah.com to: sally@blah.com", SessiongId = "123" },
  66. new LogItem() { Message = "from: rik@zoom.com to: alex@blah.com to: paul@blah.com", SessiongId = "123" },
  67. new LogItem() { Message = "from: joe@blah.com to: alex@blah.com ", SessiongId = "123" },
  68. new LogItem() { Message = "from: paul@blah.com to: Nancy@blah.com", SessiongId = "124" },
  69. new LogItem() { Message = "from: alex@blah.com to: paul@blah.com to: nancy@blah.com to: sally@blah.com", SessiongId = "124" },
  70. new LogItem() { Message = "from: nancy@blah.com to: joe@blah.com to: tim@whoville.com", SessiongId = "125" },
  71. new LogItem() { Message = "from: joe@blah.com to: alex@blah.com to: paul@blah.com", SessiongId = "125" },
  72. new LogItem() { Message = "from: alex@blah.com to: Paul@blah.com to: sally@blah.com to: nancy@blah.com", SessiongId = "126" },
  73. new LogItem() { Message = "from: Paul@blah.com to: joe@blah.com to: jim@blah.com", SessiongId = "126" },
  74. new LogItem() { Message = "from: alex@blah.com to: view@blah.com to: paul@blah.com", SessiongId = "126" },
  75. };
  76. }
Add Comment
Please, Sign In to add comment