I have a Linq to SQL query that appears to be producing the right SQL but the results are not the same as if I run the query directly in SQL Management Studio Express.
My C# code is:
using (DataClasses1DataContext dc = new DataClasses1DataContext())
{
var q = from s in dc.tbContentDetails
where s.Active == true && s.SupervisorApproved == true
select s;
foreach (string st in result2)
{
q = from s in q
where s.Fuzzy.Contains("|" + st + "|")
select s;
}
lblResults.Text = q.Count().ToString();
repResults.DataSource = q.Take(10);
repResults.DataBind();
}
The SQL generated is (field list replaced with "..."):
SELECT [t0].[ContentDetailId], ... FROM [dbo].[tbContentDetail] AS [t0]
WHERE ([t0].[Fuzzy] LIKE @p0) AND ([t0].[Fuzzy] LIKE @p1)
AND ([t0].[Fuzzy] LIKE @p2) AND ([t0].[Active] = 1)
AND ([t0].[SupervisorApproved] = @p3)
If I run this query directly I get the expected results but when it runs in Linq I get many more search results, exactly the same as if all the "Fuzzy LIKE" in the where clause are joined with OR not AND.
If I put all the "Fuzzy.Contains" in to one Linq statement it does work fine but as I need to allow for any number of keywords I chose this way of building my query. Perhaps this is not the right way to do this or is there some other problem here?