Is it possible to refactor following query so i.Title.Contains(query) || i.Description.Contains(query) is there only once while staying within single query (no subqueries) in the resulting sql?
if (extendedSearch)
{
result = result.Where(i => i.Title.Contains(query) || i.Description.Contains(query)
|| (i.CreatedBy.FirstName + " " + i.CreatedBy.LastName).Contains(query)
|| (i.AssignedTo.FirstName + " " + i.AssignedTo.LastName).Contains(query)
);
}
else
result = result.Where(i => i.Title.Contains(query) || i.Description.Contains(query));
(keeping fingers crossed that brackets are correct…)
Logical structure in Where expression: A or (extendedSearch and B)
If
extendedSearchis false the result depends only on A. IfextendedSearchis true the result depends on (A or B) – which represents the logic in your query, I hope.