I have a DataTable shown below:
RefID RefDescription ReferenceUrl SortOrder
0 Interdiscip http://www.ncbi.nlm.nih.gov/ 1
0 Entropy 20133 http://www.mdpi.com/1099-4300 2
0 Interdiscip http://www.ncbi.nlm.nih.gov/ 3
0 Agriculture http://www1.agric.gov.ab.ca/ 4
0 Interdiscip http://www.ncbi.nlm.nih.gov/ 5
In the above dataset if we have value of RefDescription and ReferenceUrl are same remove those duplicate rows and keep the single row, also append the numbers to it, according to sort Order.
Here, in the above dataSet-- RefDescription: Interdiscip is repeated three times and also it has ReferenceUrl same. so i want the following output as dataset.
Result i want:
RefID RefDescription ReferenceUrl SortOrder
0 3,5 Interdiscip http://www.ncbi.nlm.nih.gov/ 1
0 Entropy 20133 http://www.mdpi.com/1099-4300 2
0 Agriculture http://www1.agric.gov.ab.ca/ 4
Note:RefDescription and ReferenceUrl both should have same values in this scenarios otherwise no need to remove and append.
C# Code: i Tried
protected void Page_Load(object sender, EventArgs e)
{
int rowcount = 0;
DataTable dt = new DataTable("ReferenceData");
dt.Columns.Add("ReferenceID");
dt.Columns.Add("ReferenceDescription");
dt.Columns.Add("ReferenceUrl");
dt.Columns.Add("SortOrder");
dt.Rows.Add("0","Interdiscip","http://www.ncbi.nlm.nih.gov/","1");
dt.Rows.Add("0", "Entropy 20133", "http://www.mdpi.com/1099-4300", "2");
dt.Rows.Add("0", "Interdiscip", "http://www.ncbi.nlm.nih.gov/", "3");
dt.Rows.Add("0", "Agriculture", "http://www1.agric.gov.ab.ca/", "4");
dt.Rows.Add("0", "Interdiscip", "http://www.ncbi.nlm.nih.gov/", "5");
DataSet ds = new DataSet();
DataTable dtOut = null;
ds.Tables.Add(dt);
DataView dv = dt.DefaultView;
dv.Sort = "ReferenceDescription,ReferenceUrl";
dtOut = dv.ToTable();
for (int t = 0; t < dtOut.Rows.Count; t++)
{
int i = 0;
int count = 0;
int sortorder = 0;
string space = null;
string x = dtOut.Rows[t][1].ToString();
string y = dtOut.Rows[t][2].ToString();
sortorder = Convert.ToInt32(dtOut.Rows[rowcount][3]);
for (int j = 0; j < dtOut.Rows.Count; j++)
{
if (x == dtOut.Rows[i][1].ToString() && y == dtOut.Rows[i][2].ToString())
{
count++;
if (count > 1)
{
sortorder = Convert.ToInt32(dtOut.Rows[i][3]);
space += sortorder + " ";
dtOut.Rows[i].Delete();
dtOut.AcceptChanges();
}
}
i++;
}
dtOut.Rows[rowcount][1] = space + x;
rowcount++;
}
}