简体   繁体   English

寻找比较string []如果在列表中存在则拆分 <KeyValuePair<string, string> &gt; myList

[英]looking to compare string[] splitted if it exists in List<KeyValuePair<string, string>> myList

I am looking to compare string[] splitted if it exists in List<KeyValuePair<string, string>> myList . 我想比较string[] splitted如果存在于List<KeyValuePair<string, string>> myList IF it does not then I would add it to List<string> as a seperate list of email addresses. 如果没有,那么我会将其添加到List<string>作为电子邮件地址的单独列表。

using LINQ might be a good option, please help with implementaion. 使用LINQ可能是一个不错的选择,请帮助实现。

looking to compare string[] splitted if it exists in List<KeyValuePair<string, string>> myList 试图比较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));

    }
  }

} }

Use implicitly typed variable (var) if possible 如果可能,使用隐式类型的变量(var)

You can achieve this by using join 您可以使用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();
}
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;
    }

} }

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM