Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- void Main() {
- var logItems = GetLogItems();
- var Messages = logItems.Select(item => new {
- From = extractFromEmailFromMessage(item.Message),
- SentToList = extractToEmailsFromMessage(item.Message)
- }).ToList();
- var MessagesGroupByFrom = Messages
- .OrderBy(x => x.From)
- .GroupBy(x => x.From)
- .Select(x => new {
- From = x.Key,
- SentTo = x.SelectMany(y => y.SentToList)
- .GroupBy(y => y)
- .Select(g => new {
- To = g.Key,
- Count = g.Count()
- }).ToList()
- }).ToList();
- MessagesGroupByFrom.ForEach(fromEmail => {
- Console.WriteLine($"--- {fromEmail.From} ---");
- fromEmail.SentTo.ForEach(toObj => {
- Console.WriteLine($" {toObj.To} Count: {toObj.Count} ");
- });
- });
- }
- class LogItem
- {
- public string Message;
- public string SessiongId;
- public string From;
- public string To;
- }
- List<string> extractToEmailsFromMessage(string text) {
- var toPattern = @"to:\s+(\w+([-+.']\w+)*@\w+([-.]\w+)*\.\w+([-.]\w+)*)";
- var toRegex = new Regex(toPattern, RegexOptions.IgnoreCase);
- var matches = new List<string>();
- foreach (Match match in toRegex.Matches(text)) {
- matches.Add(match.Groups[1].Value?.ToLower());
- }
- return matches;
- }
- string extractFromEmailFromMessage(string text) {
- var fromPattern = @"from:\s+(\w+([-+.']\w+)*@)\w+([-.]\w+)*\.\w+([-.]\w+)*";
- var fromRegex = new Regex(fromPattern, RegexOptions.IgnoreCase);
- var match = fromRegex.Match(text);
- if (match.Success) {
- return match.Groups[1].Value.ToLower();
- }
- return "";
- }
- List<LogItem> GetLogItems() {
- return
- new List<LogItem> {
- new LogItem() { Message = "from: rik@zoom.com to: paul@blah.com to: sally@blah.com", SessiongId = "123" },
- new LogItem() { Message = "from: rik@zoom.com to: alex@blah.com to: paul@blah.com", SessiongId = "123" },
- new LogItem() { Message = "from: joe@blah.com to: alex@blah.com ", SessiongId = "123" },
- new LogItem() { Message = "from: paul@blah.com to: Nancy@blah.com", SessiongId = "124" },
- new LogItem() { Message = "from: alex@blah.com to: paul@blah.com to: nancy@blah.com to: sally@blah.com", SessiongId = "124" },
- new LogItem() { Message = "from: nancy@blah.com to: joe@blah.com to: tim@whoville.com", SessiongId = "125" },
- new LogItem() { Message = "from: joe@blah.com to: alex@blah.com to: paul@blah.com", SessiongId = "125" },
- new LogItem() { Message = "from: alex@blah.com to: Paul@blah.com to: sally@blah.com to: nancy@blah.com", SessiongId = "126" },
- new LogItem() { Message = "from: Paul@blah.com to: joe@blah.com to: jim@blah.com", SessiongId = "126" },
- new LogItem() { Message = "from: alex@blah.com to: view@blah.com to: paul@blah.com", SessiongId = "126" },
- };
- }
Add Comment
Please, Sign In to add comment