I'm trying to use Linq to SQL to search a text column for multiple matching words, but the query is not doing what I expect.
Here is my sample code:
string[] nameSearch = new string[2];
nameSearch[0] = "John";
nameSearch[1] = "Doe";
var customers = context.Customers.AsQueryable();
foreach (string name in nameSearch)
{
customers = customers.Where(r => r.CustName.Contains(name));
}
Data.Customer[] results = customers.ToArray();
The query that runs is:
SELECT [t0].[CustName]
FROM [dbo].[Customer] AS [t0]
WHERE ([t0].[CustName] LIKE @p0) AND ([t0].[CustName] LIKE @p1)
-- @p0: Input NVarChar (Size = 5; Prec = 0; Scale = 0) [%Doe%]
-- @p1: Input NVarChar (Size = 5; Prec = 0; Scale = 0) [%Doe%]
How do I fix this query? (It's suppose to be searching for John and Doe, not Doe and Doe.)