the requirement for our search query is to look for all search words in the beginning of vehicle manufacturer, model and variant name. Case insensitive. The word being a string of characters separated by space.
E.g. when search for "AU A3 TDI" you would find a vehicle with the following attributes: Vehicle { ManufacturerName = "Audi", ModelName = "A3", Name = "2.0L TDI" }
...but would not
return this vehicle:
Vehicle { ManufacturerName = "Renault", ModelName = "A3", Name = "2.0L TDI" }
because no words start with au
.
I'm using specification with LINQ Expression and the code I wrote works fine and is tested however I believe it smells a bit. It's not very readable IMHO but most importantly, there might be a clear performance improvement you could point out?
public string[] SearchTerms = searchText.Split();
Expression<Func<SearchVehicleVariant, bool>> expression =
c =>
SearchTerms.All(
s =>
string.Format(
" {0} {1} {2}",
c.ManufacturerName != null ? c.ManufacturerName.ToUpper() : string.Empty,
c.ModelName != null ? c.ModelName.ToUpper() : string.Empty,
c.Name != null ? c.Name.ToUpper() : string.Empty).Contains(
string.Concat(" ", s.Trim().ToUpper())));