Home > OS >  How to remove duplicates from the list in C#
How to remove duplicates from the list in C#

Time:07-18

Please help me to fix this issue. My dropdown list looks something like this mentioned below.

  1. Client
  2. Contractor,Contractor,Contractor,Manager
  3. Contractor,Manager
  4. Manager
  5. Operator
  6. Viewer

I want to remove the duplicates and my output should be like :

  1. Client
  2. Contractor
  3. Manager
  4. Operator
  5. Viewer

This is my code mentioned below: Property: public List<string> TeamRoleNames => TeamRoleUids.Select(MainRoles.GetRoleName).ToList();

Display Method:

            {
                result  = " ; TeamRoleNames="   this.TeamRoleNames;
            }

GetRole Method:

        {
            string roleName;

            if (RoleNameByUid.TryGetValue(roleUid, out roleName))
            {
                return roleName;
            }

            return null;
        }

I have tried with Distinct Method mentioned below, But did not work like the output what I wanted! public List<string> TeamRoleNames => TeamRoleUids.Select(MainRoles.GetRoleName).Distinct().ToList();

How can I fix this? Can anyone help?

CodePudding user response:

Having elements comma separated require you to split them first to have an homogenous collection then do the distinct

// get the comma separated values out as 1 value each
// for that you can use split, remove empty and select many
// which will return as a single level list (flat)
var result = TeamRoleUids.SelectMany(o => o.Split(new[] { ',' }, StringSplitOptions.RemoveEmptyEntries)).Distinct().ToList();

CodePudding user response:

Consider converting the list to a set (hashset) since sets as a data structure doesn't allow duplicates. More about hashsets form official documentation.

So, the solution would be similar to the following:

var hashSet = new HashSet<YourType>(yourList);

example:

var hashSet = new HashSet<string>(TeamRoleUids);

then converting it back toList() will remove duplicates.

CodePudding user response:

If you have already tried Distinct and it hasn't worked, then you could do the following;

Split your string list to a List<string>

List<string> result = TeamRoleNames.Split(',').ToList();

Then when you're adding them to the dropdwon, check to see if the role is already in the dropdown. If so, move on, else add to the dropdown.

So something like

foreach(var role in this.TeamRoleNames)
{
    if(!result.contains(role))
        result  = " ; TeamRoleNames="   role;
}

CodePudding user response:

You can use SelectMany to flatten a enumeration containing a nested enumeration. Here, we create the nested enumeration by splitting the string at the commas:

string[] input = {
    "Client",
    "Contractor,Contractor,Contractor,Manager",
    "Contractor,Manager",
    "Manager",
    "Operator",
    "Viewer"
};

var roles = input
    .SelectMany(r => r.Split(','))
    .Distinct()
    .OrderBy(r => r)
    .ToList();

foreach (string role in roles) {
    Console.WriteLine(role);
}

prints

Client
Contractor
Manager
Operator
Viewer
  • Related