0

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?

Philip
  • 1
  • 1
  • 1
  • 5
  • Does this answer your question? [Remove duplicates from a List in C#](https://stackoverflow.com/questions/47752/remove-duplicates-from-a-listt-in-c-sharp) – SᴇM Jul 18 '22 at 10:19
  • @SeM : I have already tried with Distinct method, but did not work for me. – Philip Jul 18 '22 at 10:23
  • In what way did it not work? – DavidG Jul 18 '22 at 10:23
  • Hi @DavidG : I want to remove the duplicates and my output should be like : Client Contractor Manager Operator Viewer But it returned like : 1. Client 2. Contractor, Manager 3. Manager 4. Operator 5. Viewer – Philip Jul 18 '22 at 10:24
  • @Philip so some string actually contain comma separated values ? – Franck Jul 18 '22 at 10:26
  • Yes @Franck. I dont want that to be in my output. – Philip Jul 18 '22 at 10:27
  • You need to split your subelements then – Franck Jul 18 '22 at 10:28
  • @Philip The offered solution likely did not work because of how you're checking equality (instance references vs equal property values). However, the question does not contain enough information to confirm/solve this. – Flater Jul 18 '22 at 10:28
  • @Franck : Can you please help me like how can I split subelements? – Philip Jul 18 '22 at 10:29
  • @Philip check my answer – Franck Jul 18 '22 at 10:41

4 Answers4

1

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.

Wael Moughrbel
  • 214
  • 1
  • 3
  • 11
  • Thanks for your input :) I am not much familiar with HashSet, If you could give me clear structure of explanation would help me understanding better. – Philip Jul 18 '22 at 10:59
  • @Philip I have added a link to documentation that explains HashSet as a concept and its usage with examples from the official Microsoft documentation. The trick here is that HashSet doesn't allow duplicates. So, considering you have a list of strings that has duplicates, just by converting the list you have (TeamRoleUids) to HashSet as I showed in the answer will remove duplicates. Then convert it back to list using the method ToList(). – Wael Moughrbel Jul 18 '22 at 11:19
1

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();
Franck
  • 4,438
  • 1
  • 28
  • 55
  • I am facing some error when I paste your code. – Philip Jul 18 '22 at 10:46
  • @Philip the error is unrelated from this code because it works as intended – Franck Jul 18 '22 at 10:49
  • @Philip see it perfectly running [here](https://dotnetfiddle.net/eN27iX) – Franck Jul 18 '22 at 10:52
  • Hi @Frank : Yes I saw it worked for you. Great! Please find the error below what I am facing : – Philip Jul 18 '22 at 10:56
  • A field initializer cannot reference the non-static field method or property MainUser.TeamRoleNmaes – Philip Jul 18 '22 at 10:57
  • I tried your solution, But seems like I am getting wrong output. Please check my code below: – Philip Jul 18 '22 at 12:41
  • ``` public List TeamRoleNames => TeamRoleUuids.Select(FeRoles.GetRoleName).SelectMany(o => o.Split(new[] { ',' }, StringSplitOptions.RemoveEmptyEntries)).Distinct().ToList(); ``` ```if (this.TeamRoleNames != null) { result += " ; TeamRoleNames=" + this.TeamRoleNames.SelectMany(x => x.Split(',')).Distinct(); } ``` – Philip Jul 18 '22 at 12:43
  • Hi @Franck : Are you available? – Philip Jul 19 '22 at 04:53
  • @Philip Stack overflow is not a forum. You have a question you get an answer. You have more question that's something else. You need to click the "Ask Question" button on top right and ask a new one – Franck Jul 19 '22 at 10:57
0

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;
}
JamesS
  • 2,167
  • 1
  • 11
  • 29
  • Hi @JamesS : I tried your solution, I am getting error in the line List result = TeamRoleNames.Split(',').ToList(); saying : A field initializer cannot reference the non-static field method or property MainUser.TeamRoleNmaes – Philip Jul 18 '22 at 11:02
0

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
Olivier Jacot-Descombes
  • 104,806
  • 13
  • 138
  • 188
  • Hi @Olivier : I am getting same error as mentioned below : A field initializer cannot reference the non-static field method or property MainUser.TeamRoleNmaes – Philip Jul 18 '22 at 11:03
  • If `TeamRoleNames` is an instance member, i.e., if it is not static, you need an object of the class `MainUser`. E.g. `var mainUser = new MainUser(); var roles = mainUser.TeamRoleNames;`. But probably you will have to get this object from somewhere and fill it with some data. – Olivier Jacot-Descombes Jul 18 '22 at 11:07