简体   繁体   English

如何用此代码添加现有的字符串数组值?

[英]How to add existing string array value with this code?

Variable.cs Variable.cs

 public string[] CcEmails { get; set; }

Mail.cs Mail.cs

  EDTO.CcEmails = dr["rsh_ccmail"].ToString().Split(';');

here i got two strings eg. 在这里,我有两个字符串。 xxxx@gmail.com ; xxxx@gmail.com; yyy@gmail.com yyy@gmail.com

MailProcess.cs MailProcess.cs

dataRPT1=get data from sql
EDTO.CcEmails = new string[dataRPT1.Rows.Count];
                    for (int i = 0; i < dataRPT1.Rows.Count; i++)
                    {
                        EDTO.CcEmails[i] = dataRPT1.Rows[i]["email_addr"].ToString();
                    }

Here i got list of string eg.aaa@gmail.com ...... I am try to add with existing but it add only new values..Anyone could help me.. 在这里,我得到了字符串列表,例如:aaa@gmail.com ............我尝试用现有的添加,但它仅添加新的值。任何人都可以帮助我..

I find the easiest way of doing this is to create a list, add items to the list, then use string.Join to create the new string. 我发现最简单的方法是创建一个列表,向列表中添加项目,然后使用string.Join创建新字符串。

var items = new List<string>();
for (int i = 0; i < dataRPT1.Rows.Count; i++)
{
    items.Add(dataRPT1.Rows[i]["email_addr"].ToString());
}

EDTO.CcEmails = string.Join(";", items);

Update after changed question: 更改问题后更新:

If the type of the CcEmails is an array, the last line could be: 如果CcEmails的类型是数组,则最后一行可能是:

EDTO.CcEmails = items.ToArray();

I tend to use union, although that will remove duplicate entries. 我倾向于使用并集,尽管那样会删除重复的条目。 But to keep all entries you can use Concat on the array. 但是要保留所有条目,您可以在阵列上使用Concat。

        var emailString = "me@test.com;you@test.com";
        string[] emails = emailString.Split(';');

        string[] emailsFromSQL = new string[3];
        emailsFromSQL[0] = "everyone@test.com";
        emailsFromSQL[1] = "everyone2@test.com";
        emailsFromSQL[2] = "everyone2@test.com";

        //No Duplicates
        var combined = emails.Union(emailsFromSQL).ToArray();

        //Duplicates
        var allCombined = emails.Concat(emailsFromSQL).ToArray();

Thanks 谢谢

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

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