0

我正在寻找比较string[] splitted它是否存在于List<KeyValuePair<string, string>> myList. 如果没有,那么我会将其添加List<string>为单独的电子邮件地址列表。

使用 LINQ 可能是一个不错的选择,请帮助实现。

希望比较string[] splitted它是否存在于List<KeyValuePair<string, string>>myList中

  public void SendEmails(String esmails)
{
  //splitting email string 
  string[] splitted = esmails.Emails.Split(new string[] { System.Environment.NewLine },     StringSplitOptions.RemoveEmptyEntries);

  List<KeyValuePair<string, string>> myList = new List<KeyValuePair<string, string>>();

  foreach (var email in GetEmails)
  {
    for (int i = 0; i < splitted.Count(); i++)
    {
      if (email.EmailAddress.Equals(splitted[i].Replace(";","")))
          myList.Add(new KeyValuePair<string, string>(email.IdentificationNo, email.EmailAddress));

    }
  }

}

4

2 回答 2

0
public void SendEmails(String esmails)
{
    List<String> split = esmails.Split(new String[] { System.Environment.NewLine }, StringSplitOptions.RemoveEmptyEntries).Select(s => s.Replace(";", "")).ToList();

Dictionary<Int32, String> dictionary = new Dictionary<Int32, String>();

foreach (Email email in GetEmails())
{
    foreach (String address in split)
    {
        if (email.EmailAddress.Equals(address))
            dictionary[email.IdentificationNo] = email.EmailAddress;
    }

}

于 2013-01-17T17:46:24.640 回答
0

如果可能,使用隐式类型变量 (var)

您可以通过使用 join 来实现这一点

public void SendEmails(String esmails)
{
    //splitting email string 
    var splitted = 
        esmails.Split(new []{System.Environment.NewLine}, StringSplitOptions.RemoveEmptyEntries)
        .Select(s => s.Replace(";", ""));

    var matchings = 
            from s in splitted
            join email in GetEmails on email.EmailAddress equals s
            select new KeyValuePair<string, string>(email.IdentificationNo, email.EmailAddress);

    var myList = matchings.ToList();
}
于 2013-01-17T17:14:25.807 回答