Click here to Skip to main content
15,890,506 members
Please Sign up or sign in to vote.
4.00/5 (1 vote)
See more:
HI Everyone,

Nice to See u all.

I am trying to build my logic .I ahve completed the half of the part but i got stuck into this situation.
I am searching for a For loop which delete the duplicate Records.Duplicate records in the sense For Examle:
I have a columns as follows for Excel Sheet name: Employee
Sr.No Name Surname
1 Amit podar
2 Salman khan
3 Amit podar

In this Scenario,I want to read all the records but after reading all records i want to insert only first two records because third records is duplicate that i dont want to insert in database.I want to use for loop for the same.
Kindly advice.
Thanks Harshal
Posted
Comments
King Fisher 21-May-14 6:11am    
Is this Your Task ? either, do you migrating Records from Excel?
R Harshal 21-May-14 6:20am    
yes.migrating Records From Excel.

Thanks
Harshal.

Use a dictionary object. When reading you can check the "ContainsKey" function to see if it was already added or not.

See here[^]

hope this helps.
 
Share this answer
 
Comments
R Harshal 21-May-14 7:03am    
its so advanced .Actually its good BUt in my requirement there is twist.Is there any other way to get it solve.At the time of reading i want to ignore that duplicate records not at the time of adding.

Thanks
Harshal.
V. 21-May-14 7:58am    
Why should you read the value in and delete it afterwards?
You can always do it via two object, read it in a List object and then loop through the list adding to the dictionary object with the ContainsKey check. The List object is the copy of the list and the dictionary object your filtered copy. This is not an advanced object at all...
Remove duplicate values through List Contain method
public string[] RemoveDuplicates(string[] inputArray)
{
    List<string> distinctArray = new List<string>();
    foreach (string element in inputArray)
    {
        if (!distinctArray.Contains(element))
            distinctArray.Add(element);
    }

    return distinctArray.ToArray<string>();
}

Delete Duplicate elements through ArrayList Contain method
public string[] RemoveDuplicates(string[] inputArray)
{
    System.Collections.ArrayList distinctArray = new System.Collections.ArrayList();

    foreach (string element in inputArray)
    {
        if (!distinctArray.Contains(element))
            distinctArray.Add(element);
    }

    return (string[])distinctArray.ToArray(typeof(string));

}
 
Share this answer
 
Comments
R Harshal 21-May-14 6:34am    
HI Nikhil_S
Thanks for your reply .
Actually i want if their are two duplicate records ,So i want to insert only one record from that.
The condition which you gave me is if its contains(element) then Add(element) which is not my requirement .please advice if i wrong.
Thanks Harshal
You can loop through the records and check for the existing in the database before inserting.
 
Share this answer
 

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900