This project has moved. For the latest updates, please go here.

IQueryableSearch

Versatile 'Search-All' Extension for IQueryable objects

Search()

Notes about all overloads

  • When searching strings, the search method uses Contains() and not Equals() unless specified (searching 'bc' would return true for 'abcd')
  • If types_to_explore is not specified, the search will only search the immediate object and will not traverse through embedded properties
  • If keywords is of Type object it can take in any object for search, and will only be compared against properties of their Type, ie: if you search for string "3" and there is a property with value of int '3', it will not be returned

Overload

IQueryable Search(object[] keywords)
Details:
  • Search an IQueryable's properties for the keywords (objects)
  • keywords can be of any Type, will only compare against properties that match each keyword's Type
  • this does not iterate through levels of an object
Usage:
//simple search
var simpleResults = Cars.Search(new object[] { "Leather", "2-Door" });

//build keywords for a more advanced search, against different multiple Types in an object
object[] keywords = new object[] { 1, 2, 3, new Engine() { Cylinders = 8 }, "Leather" };
var results = Cars.Search(keywords);

Overload

IQueryable Search(object[] keywords, Type[] types_to_explore)
Details:
  • Search an IQueryable's properties for the keywords (objects)
  • keywords can be of any Type, will only compare against properties that match each keyword's Type
  • this iterates down through any objects specified in types_to_explore
Usage:
//simple search
var simpleResults = Cars.Search(new object[] { "Leather", "2-Door" }, new Type[] { typeof(Engine) });

//build keywords for a more advanced search, against different multiple Types in an object
object[] keywords = new object { "V8", 350 };
Type[] explore = new Type { typeof(Engine), typeof(Size) };
var results = Cars.Search(keywords, explore);

//this will search each Car for the string 'V8' and int '350' as well as each Car.Engine and Car.Engine.Size for 'V8' and '350' as well
//any combination that might be included of these types will be searched, ie: Car.Size.Engine.Size, Car.Engine.Size.Size if they were to exist

Overload

IQueryable Search(string[] properties_to_search, object[] keywords)
Details:
  • Search an IQueryable's properties for the keywords (objects), only including the properties specified in properties_to_search
  • keywords can be of any Type, will only compare against properties that match each keyword's Type included in properties_to_search
Usage:
//simple search
var simpleResults = Cars.Search(new string[] { "Seats" }, new object[] { 2 });

//build keywords for a more advanced search, against different properites
string[] properties = new string[] { "Seats", "AC" }
object[] keywords = new object { 2, true };
var results = Cars.Search(properties, keywords);

//this will only search the 2 properties 'Seats' (which is an int) and 'AC' which is a bool
//since the 2 objects specified are of Type int and boot will search for Cars where 'Cars.Seats == 2 && AC == true'

Overload

IQueryable Search(string[] properties_to_search, string[] keywords)
Details:
  • Basic single level string only search
Usage:
//simple search
var simpleResults = Cars.Search(new string[] { "Details" }, new string[] { "fast", "budget" });

Overload

IQueryable Search(Type[] types_to_explore, object[] keywords)
Details:
  • This does not iterate down into any nested objects
  • Recommended you don't use this method as it is very limited, it is mostly used internally
Usage:
//simple search
var simpleResults = Cars.Search(new Type[] { typeof(string) }, new object[] { "Leather", "2-Door" });

//showing a more detailed example to show some limitations of this method
Type[] explore = new Type { typeof(string), typeof(Engine) };
object[] keywords = new object { "V8", 350 };
var results = Cars.Search(explore, keywords);

//note that is will *only* search the first level of car, it will *not* search Car.Engine at all
//if you wanted to compare against an Engine, you would have to add new Engine(){ ... } to your keywords

Overload

IQueryable Search(string[] properties_to_search, object[] keywords, Type[] types_to_explore)
Details:
  • The same as IQueryable Search(object[] keywords, Type[] types_to_explore) but limited to properties in properties_to_search
  • keywords can be of any Type, will only compare against properties that match each keyword's Type
  • this iterates down through any objects specified in types_to_explore
Usage:
//simple search
var simpleResults = Cars.Search(new string[] { "Details", "Power" }, new object[] { "Leather", "2-Door" }, new Type[] { typeof(Engine) });

//build keywords for a more advanced search, against different multiple Types in an object
string[] properties = new string[] { "Details", "Power" };
object[] keywords = new object { "V8", 350 };
Type[] explore = new Type { typeof(Engine), typeof(Size) };
var results = Cars.Search(keywords, explore);

//this will search each Car for the string 'V8' and int '350' in fields named "Details" and "Engine"

Overload

IQueryable Search(string[] properties_to_search, string[] keywords)
Details:
  • Basic single level string only search
Usage:
//simple search
var simpleResults = Cars.Search(new string[] { "Details" }, new string[] { "fast", "budget" });

Overload

IQueryable Search(string[] properties_to_search, string[] keywords, 
    IQueryableSearch.StringSearchType string_search_type)
Details:
  • Basic single level string only search
  • If string_search_type is set to Equals it will only count exact matches (case matters), otherwise it behaves the same as IQueryable Search(string[] properties_to_search, string[] keywords)
  • Generally not used as this extension is for more dynamic searches, but available nonetheless
Usage:
//simple search
var simpleResults = Cars.Search(new string[] { "Details" }, new string[] { "fast", "budget" },
    Naspinski.Utilities.IQueryableSearch.StringSearchType.Equals);

Overload

IQueryable Search(Dictionary<string, Type> properties_to_search, object[] keywords)
Details:
  • Recommended you don't use this method as it adds extra work that can be handled by Reflection
  • Only use if :
    • You have multiple nested properties with the same name, different type and
    • You don't want to search them both and
    • You want to do a multi-level search
Usage:
Dictionary<string, Type> properties = new Dictionary<string, Type>() { { "name", typeof(string) } };
object[] keywords = new object[] { "Corvette" };
var results = Cars.Search(properties, keywords);

Overload

IQueryable Search(Dictionary<string, Type> properties_to_search, object[] keywords,
    IQueryableSearch.StringSearchType string_search_type)
Details:
  • Recommended you don't use this method as it adds extra work that can be handled by Reflection
  • If string_search_type is set to Equals it will only count exact matches (case matters), otherwise it behaves the same as IQueryable Search(Dictionary<string, Type> properties_to_search, object[] keywords)
  • Only use if :
    • You have multiple nested properties with the same name, different type and
    • You don't want to search them both and
    • You want to do a multi-level search
Usage:
Dictionary<string, Type> properties = new Dictionary<string, Type>() { { "name", typeof(string) } };
object[] keywords = new object[] { "Corvette" };
var results = Cars.Search(properties, keywords, Naspinski.Utilities.IQueryableSearch.StringSearchType.Equals);

Last edited Dec 6, 2009 at 8:27 AM by naspinski, version 4

Comments

No comments yet.