I have a DataTable as shown below:

After using below LINQ Expression above DT:
if (dt.AsEnumerable().All(row => string.IsNullOrEmpty(row.Field<string>("SameReferences")))) BindOldReferences(dt); else { var grps = from row in dt.AsEnumerable() let RefID = row.Field<string>("ReferenceID") let RefDescription = row.Field<string>("ReferenceDescription") let ReferenceUrl = row.Field<string>("ReferenceUrl") let SortOrder = row.Field<int>("sortOrder") group row by new { RefDescription, ReferenceUrl, SortOrder } into groups select groups; dt = grps.Select(g => { DataRow first = g.First(); if (first.Field<string>("SameReferences") != null) { string duplicate = first.Field<int>("SortOrder").ToString(); first.SetField("SameReferences", string.Format("{0},{1}", duplicate, first.Field<string>("SameReferences"))); } return first; }).CopyToDataTable(); }
After applying the above LINQ to DT, it will be:

The expected DT , as shown below: exclude (,) a comma when there is one value in the Samereferences column. So, what changes should I make to LINQ to get the expected result below.

Please, help..!
user5013815
source share