Search code examples
c#linqmailaddress

I want to apply Except between two lists. One list is in MailAddress format and other is a String (Converted to list))


I want to apply Except between two lists. One list is in MailAddress format and other is a String (Converted to list))?

         List<MailAddress> toemails = new List<MailAddress>();
            //List<String> emailsFrom = address.Split(';').ToList();
            List<string> temp1 = new List<string>();
            List<MailAddress> temp2 = new List<MailAddress>();
            List<MailAddress> temp3 = new List<MailAddress>();

      //message.to gives me a list of messages (outgoing)
            foreach (var e in message.To)
            {
                temp2.Add(e);
            }

           //result contains a list of emails fetched (count=7)
            var result = from lst in ListofEmails where lst.ToLower().Length < 50 select lst;
            temp1 = result.ToList();

            //(Not able to understand this, how to proceed)

            // toemails = temp2.Except(temp1.);
            // MailMessage msg = new MailMessage();

The output, result of Except, should be a list of MailAddress objects.


Solution

  • No need to use Except, just use basic LINQ:

    Since temp1 contains the e-mail addresses...

    toemails = temp2.Where(email =>
              !temp1.Any(e => e.Equals(email.Address, StringComparison.OrdinalIgnoreCase))).ToList();