Search Extensions: Release 1.5 - Primitive type support

2 min read

Release 1.5 is here!

I've been rather busy recently with holidays and moving jobs and haven't been able to spend as much time as I'd like on SearchExtensions, however I recently jumped back on the wagon and am pleased to announce a new release.

This release means you can use SearchExtensions to search against more than just strings. New types that are now supported include (but not limited to) int, DateTime and decimal.

Each of these new types support the following search commands:

  • .EqualTo()
  • .LessThan()
  • .GreaterThan()
  • .Between()
  • .GreaterThanOrEqualTo()
  • .LessThanOrEqualTo()

In this post I will give examples on how to use each of these methods against an IQueryable<TestModel> collection named testContext.TestModels.

.EqualTo() method

This method is the same as the .IsEqual() string search method. Note: In this release the string IsEqual() method has been made obsolete and has been replaced by an .EqualTo() method in order to keep the method names consistent across the library.

var result = testContext.TestModels.Search(x => x.IntegerOne, x => IntegerTwo)
                                   .EqualTo(1, 5, 8, 12);
                                   

The above implementation will return any records where either IntegerOne or IntegerTwo equal 1, 5, 8 or 12.

.LessThan() and .LessThanOrEqualTo() method

This method allows you to return records where any of the supplied properties are less than the supplied value

var date = new DateTime(2013, 5, 13)
var result = testContext.TestModels.Search(x => x.DateOne, x => DateTwo, x => x.DateThree)
                                   .LessThan(date);
                                   

The above implementation will return any records where either DateOne or DateTwo are less than the supplied date.

.GreaterThan() and .GreaterThanOrEqual() methods

This method allows you to return records where any of the supplied properties are greater than the supplied value

var date = new DateTime(2013, 5, 13)
var result = testContext.TestModels.Search(x => x.DateOne, x => DateTwo, x => x.DateThree)
                                   .GreaterThan(date);
                                   

The above implementation will return any records where either DateOne, DateTwo or DateThree are greater than the supplied date.

.Between() method

This method allows you to return records where any of the supplied properties are greater than and less than the supplied values

var result = testContext.TestModels.Search(x => x.DecOne, x => DecTwo, x => x.DecThree)
                                   .Between(9.99, 29.99);
                                   

The above implementation will return any records where either DecOne, DecTwo or DecThree are between 9.99 and 29.99. Implementing this without SearchExtension would require something like the following:

var result = testContext.TestModels.Where(x => (x.DecOne > 9.99 && x.DecOne < 29.99)
                                            || (x.DecTwo > 9.99 && x.DecTwo < 29.99)
                                            || (x.DecThree > 9.99 && x.DecThree < 29.99))
                                            

Combining instructions

As with all of the search extensions these methods can all be combined to easily create complex queries. When used against IQueryable these extension methods with translate these commands to the datasource if possible meaning the datasource provides the filtering instead of in memory.

var result = testContext.Products.Search(x => x.Title, x => x.SubTitle, x => x.Description)
                                 .Containing("demo", "example", "search")
                                 .Search(x => x.USPrice, x => x.EuroPrice, x => x.GBPPrice)
                                 .Between(9.99, 29.99)
                                 .Search(x => x.StartDate)
                                 .LessThanOrEqualTo(DateTime.Today);

If you would like to know more about this or any other feature, please get in touch by adding a comment below, contact me on twitter (@ninjanye) or you can raise an issue on the github project page

<p class="nuget-badge"><code>PM> Install-Package NinjaNye.SearchExtensions</code></p>