LINQ Multiple LIKE based on List

Posted by Bathan on Stack Overflow See other posts from Stack Overflow or by Bathan
Published on 2010-05-27T13:25:19Z Indexed on 2010/05/27 13:41 UTC
Read the original article Hit count: 199

Filed under:

I have a list of keywords in an ArrayList and I wanted to be able to build a query to find records in a table based on this keywords.

Since the list of keywords is dynamic I cannot build a fixed query here.

if I do something like this:

foreach (string kw in keywords)
{
  query = query.Where(p => p.Name.StartsWith(kw));
}

The "StartsWith" is required here because I need to search those records that actually start with the provided keyword.

In T-SQL it Would be something like this

Select * from Table where 
Name like 'keyword1%' 
or Name like 'keyword2%'
or Name like 'keyword3%'
or ...

But I need to be able to do this in LINQ...Is this possible?

© Stack Overflow or respective owner

Related posts about linq-to-sql