0

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.

Linh Dao
  • 1,305
  • 1
  • 19
  • 31
  • What kind of properties are you hoping to search for in order to exclude from `temp2` ? You obviously can't use `temp2` as a class because you'd be comparing `List` to `List`. – Mark C. Jun 05 '19 at 00:22
  • can you describe your problem properly – sayah imad Jun 05 '19 at 00:30
  • You need to either iterate over temp1 and create a new list of MailAddress objects (im assuming contain a property for email address, which is what you seem to be comparing), or you can do a .Select on temp2 to make a new list of string values (email address). Then do your except, because you will have two lists of the same type. – BioSafety Jun 05 '19 at 03:10
  • So basically i am extracting a list of email addresses from database and storing them in list format. Message.to contains all email address which are in "To". I want to use Except so that i will get rid of those emails from To which are already present in the list extracted from database. (Description) @sayahimad – RUTUJ PURANIK Jun 05 '19 at 04:56
  • @BioSafety- can you help with the select query, not able to figure out. – RUTUJ PURANIK Jun 05 '19 at 05:00

1 Answers1

0

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();
Mark Z.
  • 2,127
  • 16
  • 33