A simpler and more correct solution (then leppie's):
public IQueryable<Part> SearchForParts(string[] query)
{
var q = db.Parts.AsQueryable();
foreach (string qs in query)
{
q = q.Where(x => x.partName.Contains(qs));
}
return q;
}
This will work as long as partName
is a string (or an SQL equivalent of a string).
The important thing to note is partName.Contains(qs)
is different than query.Contains(partName)
.
With partName.Contains(qs)
, partName
is searched for any occurrence of qs
. The resulting SQL would be equivalent (where <qs> is the value of qs
):
select * from Parts where partName like '%<qs>%';
Also of note are StartsWith
and EndsWith
which are similar to Contains
but look for the string in the specific location.
query.Contains(partName)
is the same as a SQL in
command. The resulting SQL would be equivalent to (where <query0> is the value of query[0]
, <query1> is the value of query[1]
, and <queryN> is the last value in the query array):
select * from Parts where partName in ( <query0>, <query1>, ..., <queryN> );
Update:
It is also important to note that leppie's answer does not escape the wildcard characters before adding them to the like statement. This is not an issue with the Contains
solution since Linq will escape the query before sending it. An escaped version of the SqlMethods.Like
solution would be:
public IQueryable<Part> SearchForParts(string[] query)
{
var q = db.Parts.AsQueryable();
foreach (var qs in query)
{
string escaped_bs = qs.Replace("/", "//"),
escaped_us = escaped_bs.Replace("_", "/_"),
escaped_p = escaped_us.Replace("%", "/%"),
escaped_br = escaped_p.Replace("[", "/["),
likestr = string.Format("%{0}%", escaped_br);
q = q.Where(x => SqlMethods.Like(x.partName, likestr, '/'));
}
return q;
}
You don't have to worry about ' since Linq will escape that for you.