Use Any
:
var matchedProds = (
from p in prods
where p.searchTerms.Any(st => st.searchTerm == term)
select p
).ToList();
Question
I have tried to modify the solution Search Feature using .Contains with terms within a one field but need some help.
I have an object structure of:
public class product
{
public string productID {get;set;}
public List<searchTerms> searchTerms {get; set;}
}
public class searchTerms
{
public string searchTerm {get; set;}
}
I want to return a new List where the searchTerm is matched. I've tried:
matchedProds = (
from p in prods
where p.searchTerms.Contains(term)
select p
).ToList();
But cannot make it work with either term as a literal string or as a searchTerm.
Where am I going wrong?
Thanks
La solution 2
Use Any
:
var matchedProds = (
from p in prods
where p.searchTerms.Any(st => st.searchTerm == term)
select p
).ToList();
Autres conseils
What about:
var products = allProducts.Where(m => m.searchTerms.Any(n => n.searchTerm == input)).ToList()