Search code examples
c#linq

Filtering nested list is not bringing back the correct result set


I currently have a list of skills which looks something like:

var list = new List<Test>
    {
        new Test
        {
            Name = "Some Name",
            Skills = new List<Skill>
            {
                new Skill { SkillName = "Java" },
                new Skill { SkillName = "JavaScript" },
                new Skill { SkillName = "C#" },
                new Skill { SkillName = "CSS" }
            }
        }
    };

Now, I am trying to filter the result by the search term Java for which the code looks something like:

var searchTerm = "Java";
    
var filteredList = list.Where(t => t.Skills.Any(s => s.SkillName.Contains(searchTerm))).ToList();

Here, I would expect the filteredList to contain Java & JavaScript but it's bringing back all the 4 items. Does anyone know where I'm going wrong?

dotnetfiddle


Solution

  • You need to create a new list to achieve this. You could do this as follows:

    var filteredList =
        // where filters down the list to the tests you are interested in(ones containing a skill that has the search term)
        list.Where(t => t.Skills.Any(s => s.SkillName.Contains(searchTerm)))
            // select creates a new list, where each test only has the skills that match the search term
            .Select(t => new Test
                         {
                           Name = t.Name,
                           Skills = t.Skills.Where(s => s.SkillName.Contains(searchTerm)).ToList()
                         })
            .ToList();