Remove duplicates from a semi colon delimited string

Hi Everyone,

I am writing a BPM , part of which is creating a comma delimited string of email addresses. This is working as it should but can have a number of duplicates. Eventually this string is placed in the ‘To’ field of the email widget. I would like to remove the duplicates and have tried using ‘Distinct’ and ‘Split’ but I just keep getting errors. Do I need to add something in ‘Using’ or ‘References’ to do this?

SalesRep = (from SalesRep_Row in Db.SalesRep
    						where (SalesRep_Row.Company == Session.CompanyID
        				&& SalesRep_Row.SalesRepCode == salesRepCode)
    						select SalesRep_Row).FirstOrDefault();

								if (SalesRep != null)
								{
									if (emailRecipients == "")
										{
											emailRecipients = SalesRep.EMailAddress;
										}
									else
										{
											emailRecipients = emailRecipients + "; " + SalesRep.EMailAddress;
										}
								callContextBpmData.Character03 = emailRecipients;
/*
								msg = "Email Recipients are " + emailRecipients; 
								this.PublishInfoMessage(msg, Ice.Common.BusinessObjectMessageType.Information, Ice.Bpm.InfoMessageDisplayMode.Individual, "SalesRep", "Update");
*/
								}

Thanks in advance
Adrian.

1 Like

Hi, try this:

var l = "text1;text2;text3;text1;text4;text1".Split(';').ToList();
var qry = l.GroupBy(x => x).Select(y => y.FirstOrDefault());

foreach(var o in qry)
{
	Console.WriteLine(o);
}

Output:
text1
text2
text3
text4

4 Likes

Thanks @carlosqt, used your suggestion and ended up with this, which works how I want it to

 if (emailRecipients == "")
										{
											emailRecipients = SalesRep.EMailAddress;
										}
									else
										{
											emailRecipients = emailRecipients + "," + SalesRep.EMailAddress;									
											var sendTo = emailRecipients.Split(',').ToList();
											var qry = sendTo.GroupBy(x => x).Select(y => y.FirstOrDefault());
											var result = String.Join(",", qry);
											emailRecipients = result;
										}
								callContextBpmData.Character03 = emailRecipients;

Thanks Adrian.

2 Likes