Victor Charles
asked on
Help with deleting records with two duplicate columns using VB.NET
Hi,
How do you delete duplicate records if SN and AGD have same information using VB.NET? For example if the following file contains:
<?xml version="1.0" encoding="utf-8" standalone="yes"?>
<NewDataSet>
<AOP6A>
<SN>10411</SN>
<AGD>CAR</AGD>
<CHPT>104</CHPT>
<RIC>DEALER</RIC>
</AOP6A>
<AOP6A>
<SN>10411</SN>
<AGD>CAR</AGD>
<CHPT>104</CHPT>
<RIC>DEALER</RIC>
</AOP6A>
<AOP6A>
<SN>10412</SN>
<AGD>CAR</AGD>
<CHPT>104</CHPT>
<RIC>DEALER</RIC>
</AOP6A>
</NewDataSet>
I would like to have the following file:
<?xml version="1.0" encoding="utf-8" standalone="yes"?>
<NewDataSet>
<AOP6A>
<SN>10411</SN>
<AGD>CAR</AGD>
<CHPT>104</CHPT>
<RIC>DEALER</RIC>
</AOP6A>
<AOP6A>
<SN>10412</SN>
<AGD>CAR</AGD>
<CHPT>104</CHPT>
<RIC>DEALER</RIC>
</AOP6A>
</NewDataSet>
Thanks,
Victor
How do you delete duplicate records if SN and AGD have same information using VB.NET? For example if the following file contains:
<?xml version="1.0" encoding="utf-8" standalone="yes"?>
<NewDataSet>
<AOP6A>
<SN>10411</SN>
<AGD>CAR</AGD>
<CHPT>104</CHPT>
<RIC>DEALER</RIC>
</AOP6A>
<AOP6A>
<SN>10411</SN>
<AGD>CAR</AGD>
<CHPT>104</CHPT>
<RIC>DEALER</RIC>
</AOP6A>
<AOP6A>
<SN>10412</SN>
<AGD>CAR</AGD>
<CHPT>104</CHPT>
<RIC>DEALER</RIC>
</AOP6A>
</NewDataSet>
I would like to have the following file:
<?xml version="1.0" encoding="utf-8" standalone="yes"?>
<NewDataSet>
<AOP6A>
<SN>10411</SN>
<AGD>CAR</AGD>
<CHPT>104</CHPT>
<RIC>DEALER</RIC>
</AOP6A>
<AOP6A>
<SN>10412</SN>
<AGD>CAR</AGD>
<CHPT>104</CHPT>
<RIC>DEALER</RIC>
</AOP6A>
</NewDataSet>
Thanks,
Victor
ASKER
Can you pleaze send me the source code.
Victor
Victor
Try...
Private Sub Form1_Load(sender As System.Object, e As System.EventArgs) Handles MyBase.Load
Dim a = New DataSet()
a.ReadXml("E:\pawan.xml")
Dim b = a.Tables(0).AsEnumerable().[Select](Function(d) New With { _
Key .SN = d.Field(Of String)("SN"), _
Key .AGD = d.Field(Of String)("AGD"), _
Key .CHPT = d.Field(Of String)("CHPT"), _
Key .RIC = d.Field(Of String)("RIC") _
}).Distinct().ToList()
Console.WriteLine()
End Sub
ASKER
Hi,
To avoid writing the fields names, is there a way to modify the code for identical records instead of the two fields?
Thanks,
Victor
To avoid writing the fields names, is there a way to modify the code for identical records instead of the two fields?
Thanks,
Victor
Ok.. try
Private Sub Form1_Load(sender As System.Object, e As System.EventArgs) Handles MyBase.Load
Dim a = New DataSet()
a.ReadXml("E:\pawan.xml")
'Dim b = a.Tables(0).AsEnumerable().[Select](Function(d) New With { _
' Key .SN = d.Field(Of String)("SN"), _
' Key .AGD = d.Field(Of String)("AGD"), _
' Key .CHPT = d.Field(Of String)("CHPT"), _
' Key .RIC = d.Field(Of String)("RIC") _
'}).Distinct().ToList()
Dim k As DataTable
k = a.Tables(0)
Dim result = k.AsEnumerable().Distinct(System.Data.DataRowComparer.[Default]).ToList()
Console.WriteLine()
End Sub
ASKER CERTIFIED SOLUTION
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
ASKER
Hi Fernando,
Thank you for the solution.
Hi Pawan,
Using your approach, how do you save the results to an xml file?
Thanks,
Victor
Thank you for the solution.
Hi Pawan,
Using your approach, how do you save the results to an xml file?
Thanks,
Victor
Hi Victor;
The solution by Pawan does not meet the requirements as posted by not using Tag names such as SN, AGD, CHPT and RIC.
The solution by Pawan does not meet the requirements as posted by not using Tag names such as SN, AGD, CHPT and RIC.
ASKER
Thank You.
Not a problem Victor, glad to help.
@Fernando Soto-
Have you seen my first solution? See below- , I had used the tags, But later the Author told me not to use them.
Dim b = a.Tables(0).AsEnumerable() .[Select]( Function(d ) New With { _
Key .SN = d.Field(Of String)("SN"), _
Key .AGD = d.Field(Of String)("AGD"), _
Key .CHPT = d.Field(Of String)("CHPT"), _
Key .RIC = d.Field(Of String)("RIC") _
}).Distinct().ToList()
Have you seen my first solution? See below- , I had used the tags, But later the Author told me not to use them.
Dim b = a.Tables(0).AsEnumerable()
Key .SN = d.Field(Of String)("SN"), _
Key .AGD = d.Field(Of String)("AGD"), _
Key .CHPT = d.Field(Of String)("CHPT"), _
Key .RIC = d.Field(Of String)("RIC") _
}).Distinct().ToList()
ASKER
Hi Pawan,
I wanted to compared two fields without including all the fields in my code.
Thanks,
Victor
I wanted to compared two fields without including all the fields in my code.
Thanks,
Victor
ds = new DataSet()
ds.ReadXml(xml file path)
use linq to filter duplicate rows..