Search code examples
c#linq

Lambda/Linq with Contains criteria for multiple keywords


I have to lists

  1. Main list with Comments field
  2. List of keywords to search for.

I want to search the keywords in each Comment field of each record which in SQL would look like below

select * from MainList
where Comment like '%keyword1%'
or Comment like '%keyword2%'
... so on until the last keyword.

So far I have seen examples but usually for single keyword at a time only e.g. LINQ with non-lambda for Any Contains

What I would like is to search each record in MainList for any instance of any of my keyowrds all at once. something like:

var newList = MainList.Where(m => m.Comments.Contains(purposes))

I would prefer doing it in lambda syntax but if not possible, linq is also okay.


Solution

  • Answering for my and others' reference.

    var newList = MainList.Where(m => keywords.Any(k => m.Comments.Contains(k))).ToList();