I have a List<string
> of variable count, and I want to query (via LINQ) a table to find any items that contain any of those strings in the Text column.
Tried this (doesn't work):
items = from dbt in database.Items
where (stringList.FindAll(s => dbt.Text.Contains(s)).Count > 0)
select dbt;
Query would be something like:
select * from items where text like '%string1%' or text like '%string2%'
Is this possible?
Using:
Search function is:
kind of new to the whole LINQ to SQL game, but does this syntax help?
Got it from:
http://blog.wekeroad.com/2008/02/27/creating-in-queries-with-linq-to-sql/
Check this article out to do what you want:
http://www.albahari.com/nutshell/predicatebuilder.aspx
This works like a dream. I essentially cut and pasted their code and got this back (with my own data-scheme of course):
Here is the code I ran for the proof of concept:
I'd suggest going to the site for the code and explanation.
(I am leaving the first answer because it works well if you need an IN statement)
After reading this post, looking for the same solution as you, I found a solution using the
.Any
and.All
methods for Linq are a nice simple and elegant way to get matching results for arrays.In this instance I'm using a search input, separated by commas as an example. I don't care if the match is not in the same case.
First Get some data to query, from Linq to SQL or wherever
Using the lambda syntax for nice tight code, and result in matches to
.Any
string in the query to either the name or description in the table.If you want only results where all strings are matched within any field you can replace
.Any
with.All
: