score:1

Accepted answer

You can use String.Trim method like this:-

first.SetField("SameReferences", string.Format("{0},{1}", duplicate, 
                first.Field<string>("SameReferences")).Trim(','));

It will remove all the trailing comma.

score:0

Try this:

if (first.Field<string>("SameReferences") != null)
{
    string duplicate = first.Field<int>("SortOrder").ToString();
    string sameReference = first.Field<string>("SameReferences");
    if (String.IsNullOrEmpty(sameReference))
        first.SetField("SameReferences", duplicate);
    else
        first.SetField("SameReferences", string.Format("{0},{1}", duplicate, sameReference));
}

Related Query

More Query from same tag