65.9K
CodeProject is changing. Read more.
Home

Karmencita: an object query language for .NET

starIconstarIconstarIconstarIcon
emptyStarIcon
starIcon

4.42/5 (15 votes)

Mar 23, 2006

viewsIcon

69355

downloadIcon

208

Learn more about Karmencita, an object query language for .NET.

Introduction

Karmencita is an object query language for .NET. Its purpose is to allow easy querying from in-memory structured data.

Features:

  • an easy, SQL-like language.
  • common, slim API used for querying data.
  • supports any IEnumerable data source, DataTables and XmlDataDocuments.
  • extensible implementation.
  • common API, but still gets results depending on the data source. (For instance, when querying XmlDataDocuments, we get back a XmlElement[]. But if we query a DataTable, we get back a DataRow[]).
  • supports IComparable for custom type implementation.

The basic idea is to have an easy SQL-like language in which we can write queries against any data source.

Using the code

Let's see some code samples:

  • query the list of running processes:
    // get an array of running processes
    Process[] proc = Process.GetProceses();
    
    // initialize Karmencita with
    // the type of object to be queries
    ObjectQuery<Process> oq = 
            new ObjectQuery<Process>();
    
    // write the query
    string query = "BasePriority > 3 and Responding" + 
                   " = true and MainWindowTitle Like C%";
    
    //run the query
    Process[] processes = (Process[]) oq.Select(proc, query);
  • query a Stack<> of Customer objects:
    // initialize the data source
    // (in this case a Stack of Customers)
    Stack<Customer> proc = .....
    
    // initialize Karmencita with
    // the type of object to be queries
    ObjectQuery<Customer> oq = 
              new ObjectQuery<Customer>();
    
    // write the query
    string query = "Name = [Thor the Mighty]" + 
                   " and IsMale = true and BirthDate" + 
                   " < [1,1,1910]";
    
    //run the query
    Customer[] processes = (Customer[]) oq.Select(proc, query);
  • query a DataTable:
    // load the DataTable from the database
    DataTable dt = .....
    
    // initialize Karmencita with
    // the type of object to be queries
    ObjectQuery<DataTable> oq = 
             new ObjectQuery<DataTable>();
    
    // write the query
    string query = "ProductName = Fish and" + 
                   " UnitPrice > 200 and IsInStock = true";
    
    //run the query
    DataRow[] processes = (DataRow[]) oq.Select(proc, query);
  • query an XMLDataDocument:
    // load the XML document
    DataTable dt = .....
    
    // initialize Karmencita with the type
    // of object to be queries
    ObjectQuery<XMLDataDocument> oq = 
        new ObjectQuery<XMLDataDocument>();
    
    // write the query
    string query = "ProductName = Fish and" + 
                   " UnitPrice > 200 and IsInStock = true";
    
    //run the query
    XmlElement[] processes = (XmlElement[]) oq.Select(proc, query);

Conclusion

As you can see, Karmencita is a generic query engine which can be used to query multiple data sources. Just throw a data source and a query to it, and have the result. No need to muck around with multiple query syntaxes (DataTable .Select, XQuery etc.). One generic query language to rule them all. :)