Click here to Skip to main content
15,891,184 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
I have one Employ class which has Name and contect.I have to remove duplicate name.Output should be Ram and his all contect number like that for all. Dont want to repeat name every time.

       List<Employ> emp = new List<Employ>();
            Employ e = new Employ();
            emp.Add(new Employ { Name = "Ram", Contect = "5454454" });
            e.Name = "Ravi";
            e.Contect = "458794";
            emp.Add(new Employ { Name = "Ravi", Contect = "4578456" });

            e.Name = "jack";
            e.Contect = "564545655";
            emp.Add(new Employ { Name = "Jack", Contect = "457844544" });
            e.Name = "Ram";
            e.Contect = "457875";
            emp.Add(new Employ { Name = "Ram", Contect = "45784554" });
            e.Name = "Ravi";
            e.Contect = "5467846554";
            emp.Add(new Employ { Name = "Ravi", Contect = "5784545" });
            e.Name = "jack";
            e.Contect = "457855664";
            emp.Add(new Employ { Name = "Jack", Contect = "4547787787" });

}
 class Employ
    {
       public string Name { get; set; }
        public string Contect { get; set; }


   
    }


What I have tried:

I removed the duplicate name entry
public static  void RemoveDuplicate(List<Employ> name) {
    List<string > str=null;
    str = name.Select(x => x.Name).Distinct().ToList();

but no idea how to contact value into this
Posted
Updated 1-May-17 0:26am

1 solution

1) the variable e is assigned multiple times but never used?

2) I would start like this

void Main()
{
	List<Employ> emp = new List<Employ>();
	emp.Add(new Employ { Name = "Ram", Contect = "5454454" });
	emp.Add(new Employ { Name = "Ravi", Contect = "4578456" });
	emp.Add(new Employ { Name = "Jack", Contect = "457844544" });
	emp.Add(new Employ { Name = "Ram", Contect = "45784554" });
	emp.Add(new Employ { Name = "Ravi", Contect = "5784545" });
	emp.Add(new Employ { Name = "Jack", Contect = "4547787787" });

    //emp.Dump(); .Dump() is a linqpad command
	
	var employesDistinctByName = emp.GroupBy(e => e.Name).Select(y => y.First());
	
	//employesDistinctByName.Dump();
}


so you get rid of the double names without destroying the list. However you did not specify how to handle the combination of Name and Contect (if the name is the same, the Contect different, how to handle this)
 
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