Q: Show results based on the searchstring in ASP.net core I have a list named models which contains another list inside it named variants. I want to perform getbysearchString method where variant names are filtrerd with searchstring passed in the url.
Repository.cs
_list.Add(new ModelSearch
            {
                ID = 1,
                Title = "Scorpio",
                Variants=new List<Variant> { new Variant {ID=1,ModelID=1,Title="Scorpio VLX refresh" },
                    new Variant { ID=2,ModelID=1,Title="Scorpio LX refresh" },
                    new Variant { ID=3,ModelID=1,Title="Scorpio3 S2" }
                }

            });

            _list.Add(new ModelSearch
            {
                ID = 2,
                Title = "Bolero",
                Variants = new List<Variant> { new Variant {ID=1,ModelID=2,Title="T4" },
                    new Variant { ID=2,ModelID=2,Title="T4+" },
                    new Variant { ID=3,ModelID=2,Title="T6" },
                    new Variant { ID=3,ModelID=2,Title="T6+" }
                }
            });
//Search method
 public IEnumerable<ModelSearch> GetBySearchString(string searchString)
        {
            return _list.Where(m => m.Title.ToLower().Contains(searchString)|| m.Variants.Contains(searchString)).AsEnumerable();
        }

ModelSearchController.cs
public class ModelSearchController : Controller
    {

        private IRepository _repo;


        public ModelSearchController()
        {
            _repo = new Repository.Repository();
        }

        public IActionResult Search(string searchString)
        {
            var filteredList = _repo.GetBySearchString(searchString);
            return View(filteredList);
        }
    }

Here is my **Index.cshtml** file:

@model IEnumerable<myApp.Models.ModelSearch>

<table class="table">
    <tbody>
        @foreach (var item in Model)
        {
            @foreach (var variant in item.Variants)
            {
                <tr>
                    <td>
                       @Html.DisplayFor(modelItem => variant.Title)
                    </td>


                </tr>
            }
        }
        </tbody>
</table>

Please help me how to achieve this.

A: Your linq query is incorrect. It just lower the case of the title, but not for searchString. Also, it didn't check the title of variants
public IEnumerable<ModelSearch> GetBySearchString(string searchString)
{
    return _list.Where(
        m => m.Title.Contains(searchString, StringComparison.InvariantCultureIgnoreCase) ||
        m.Variants.Any(v => v.Title.Contains(searchString, StringComparison.InvariantCultureIgnoreCase))
    ).AsEnumerable();
}

UPDATED - filter out variants that title does not contain the searchString
public IEnumerable<ModelSearch> GetBySearchString(string searchString)
{
    return _list.Where(
        m => m.Title.Contains(searchString, StringComparison.InvariantCultureIgnoreCase) ||
        m.Variants.Any(v => v.Title.Contains(searchString, StringComparison.InvariantCultureIgnoreCase))
    )
    .Select( m =>
        new ModelSearch
        {
            ID = m.ID,
            Title = m.Title,
            Variants = m.Variants.Where(v => v.Title.Contains(searchString, StringComparison.InvariantCultureIgnoreCase)).ToList()
        }
    )
    .AsEnumerable();
}

