I have a search string that can be "sub1 sub2 sub3"
and I want to write a proper Expression<Func<T, bool>>
that can find "sub1"
, "sub2"
, "sub3"
and "sub1 sub2 sub3"
in the x.Name
In the other hand I want to modify x.Name.ToLower().Contains(productParams.Search)
for my purpose.
Now I can search the term "sub1 sub2 sub3"
.
However, I want to search for sub-strings as well.
my expectation for the search is: "sub1" || "sub2" || "sub3" || "sub1 sub2 sub3"
productParams.Search = "sub1 sub2 sub3"
How can do it?
public class ProductsSpecification : BaseSpecifcation<Product>
{
public ProductsSpecification(ProductSpecParams productParams) : base(x =>
(string.IsNullOrEmpty(productParams.Search) ||
x.Name.ToLower().Contains(productParams.Search)) &&
(!productParams.BrandId.HasValue || x.ProductBrandId == productParams.BrandId))
}
BaseSpecifcation:
public class BaseSpecifcation<T> : ISpecification<T>
{
public BaseSpecifcation(Expression<Func<T, bool>> criteria)
{
Criteria = criteria;
}
public Expression<Func<T, bool>> Criteria { get; }
}