I have an IQueryable
extension in my User
model where I'm using LINQKit :
public static IQueryable<User> ApplyFilterTerms(this IQueryable<User> query, string search)
{
if (string.IsNullOrWhiteSpace(search))
return query;
var searchTerms = search.Split(' ').Where(x => x.Length > 0).ToArray();
if (!searchTerms.Any())
return query;
var termsPredicate = searchTerms.Aggregate(
LinqExtensions.True<User>(),
(current, term) => current.And(
LinqExtensions.False<User>()
.Or(x => x.FirstName.Contains(term))
.Or(x => x.LastName.Contains(term))
.Or(x => x.UserName.Contains(term))
));
return query.Where(termsPredicate);
}
When executing this db query, LinQ translates it into something in this pattern:
DECLARE @__term_0 AS VARCHAR(100) = 'jonh'
SELECT [u].[UserId], [u].[FirstName], [u].[LastName], [u].[UserName]
FROM [Users] AS [u]
WHERE (((@__term_0 LIKE N'') OR (CHARINDEX(@__term_0, [u].[FirstName]) > 0))
OR ((@__term_0 LIKE N'') OR (CHARINDEX(@__term_0, [u].[LastName]) > 0)))
OR ((@__term_0 LIKE N'') OR (CHARINDEX(@__term_0, [u].[UserName]) > 0))
ORDER BY [u].[FirstName], [u].[LastName]
Would there be a way to remove this apparently useless (@__term_0 LIKE N'')
that linq adds to the query?