C#C
C#•4y ago
malkav

Help refactor please? I might have missed something big here [Answered]

is there maybe a better way of writing this?
foreach (Experience experience in User.Profile.Experiences)
{
    foreach (string method in experience.Methods)
    {
        foreach (string tag in Assignment.Tags)
        {
            _matchingTags.Add( new()
            {
                Tag = method, 
                Matches = string.Equals(method, tag, StringComparison.CurrentCultureIgnoreCase) 
                          || tag.ToLower().Contains(method.ToLower()) 
                          || method.ToLower().Contains(tag.ToLower())
            });    
        }
        
    }
}


The idea here is that I have a list of an object, of which one of its properties (or fields) is Methods
and I've got a second list of strings, which are the Tags
I got to find the matching strings between them (with fuzzy preferably)
here's an example list of the two:
public class Experience
{
  // Equals and GetHashCode methods
  // Other props
  public List<string> Methods {get;set;}
}

private List<Experience> _experiences = new()
{ Methods = new() { ".NET", ".NET6.0", "C#" } };
private List<string> _tags = new()
{ ".NET", "C#", "Blazor", "Other" };

Basically I have to create a method that calculates the matching "tags" so to speak between the two lists.

Oh, additional note: I made a class MatchingTags that looks like this (just because I need both properties in the rest of the program)
public class MatchingTags
{
  public string Tag {get;set;}
  public bool Matches {get;set;}
}
Was this page helpful?