【问题标题】:I want to apply Except between two lists. One list is in MailAddress format and other is a String (Converted to list))我想在两个列表之间申请除外。一个列表是 MailAddress 格式,另一个是字符串(转换为列表))
【发布时间】:2019-10-20 12:16:09
【问题描述】:

我想在两个列表之间应用Except。一个列表是MailAddress 格式,另一个是String(转换为列表))?

         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();

Except 的输出结果应该是MailAddress 对象的列表。

【问题讨论】:

  • 您希望搜索什么样的属性以便从temp2 中排除?您显然不能将temp2 用作一个类,因为您会将List&lt;MailAddres&gt;List&lt;string&gt; 进行比较。
  • 你能正确描述你的问题吗
  • 您需要遍历 temp1 并创建一个新的 MailAddress 对象列表(我假设包含电子邮件地址的属性,这似乎是您正在比较的),或者您可以执行 .Select在 temp2 上创建一个新的字符串值列表(电子邮件地址)。然后做你的除外,因为你将有两个相同类型的列表。
  • 所以基本上我是从数据库中提取电子邮件地址列表并以列表格式存储它们。 Message.to 包含“收件人”中的所有电子邮件地址。我想使用 except ,这样我就可以摆脱那些来自 To 的电子邮件,这些电子邮件已经存在于从数据库中提取的列表中。 (描述)@sayahimad
  • @BioSafety- 你能帮忙选择查询吗,不知道。

标签: c# linq mailaddress


【解决方案1】:

无需使用Except,只需使用基本的LINQ:

因为 temp1 包含电子邮件地址...

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

【讨论】:

    猜你喜欢
    • 2012-11-12
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2021-01-06
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多