SearchExtensions : Simple Search

Library of IQueryable and IEnumerable extension methods to perform searching

This project is avaliable for download as a nuget package

PM> Install-Package NinjaNye.SearchExtensions

Simple Searching

Simple search allows you to search against more than just strings. New types that are now supported include (but not limited to) int, DateTime and decimal.

Methods

All simple search methods are available to both IQueryable data and IEnumerable data:


.EqualTo() method

This method is the same as the .EqualTo() string search method and returns records where any of the supplied properties equals any of the supplied search terms

var result = testContext.TestModels.Search(x => x.IntegerOne,
                                           x => 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.

testContext.Products.Search(x => x.Title, x => x.SubTitle)
                    .Containing("demo", "example", "search")
                    .Search(x => x.Doller, x => x.Euro, x => x.GBP)
                    .Between(9.99, 29.99)
                    .Search(x => x.StartDate)
                    .LessThanOrEqualTo(DateTime.Today);

If you have any new feature requests, questions, or comments, please get in touch, either, via my website, through twitter: @ninjanye or by creating an issue through the projects github page .