Hi mahjoubi,
Refer below code.
C#
DataTable dt = new DataTable();
dt.Columns.Add("Item");
dt.Columns.Add("Qty");
dt.Columns.Add("Price");
dt.Rows.Add("a", 1, 10);
dt.Rows.Add("b", 2, 20);
dt.Rows.Add("c", 3, 30);
dt.Rows.Add("", 3, 0);
dt.Rows.Add("", 4, 0);
dt.Rows.Add("a", 4, 0);
List<string> duplicates = (from dr in dt.AsEnumerable()
group dr by dr["Qty"] into groups
where groups.Count() > 1
select groups.Key.ToString()).ToList();
for (int i = dt.Rows.Count - 1; i >= 0; i--)
{
if (string.IsNullOrEmpty(dt.Rows[i][0].ToString()))
{
if (duplicates.Contains(dt.Rows[i][1].ToString()))
{
dt.Rows[i].Delete();
}
}
}
VB.Net
Dim dt As DataTable = New DataTable()
dt.Columns.Add("Item")
dt.Columns.Add("Qty")
dt.Columns.Add("Price")
dt.Rows.Add("a", 1, 10)
dt.Rows.Add("b", 2, 20)
dt.Rows.Add("c", 3, 30)
dt.Rows.Add("", 3, 0)
dt.Rows.Add("", 4, 0)
dt.Rows.Add("a", 4, 0)
Dim duplicates = (From dr In dt
Group dr By Qty = dr("Qty") Into Group, Count
Where Count > 1
Select New With {Key Qty}.Qty).ToList()
For i As Integer = dt.Rows.Count - 1 To 0 Step -1
If String.IsNullOrEmpty(dt.Rows(i)(0).ToString()) Then
If duplicates.Contains(dt.Rows(i)(1).ToString()) Then
dt.Rows(i).Delete()
End If
End If
Next
Output
Item | Qty | Price |
a |
1 |
10 |
b |
2 |
20 |
c |
3 |
30 |
a |
4 |
0 |