Click here to Skip to main content
Licence 
First Posted 23 Mar 2006
Views 44,553
Bookmarked 26 times

Karmencita: an object query language for .NET

By | 23 Mar 2006 | Article
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. :)

License

This article has no explicit license attached to it but may contain usage terms in the article text or the download files themselves. If in doubt please contact the author via the discussion board below.

A list of licenses authors might use can be found here

About the Author

Marius Gheorghe



United States United States

Member



Sign Up to vote   Poor Excellent
Add a reason or comment to your vote: x
Votes of 3 or less require a comment

Comments and Discussions

 
You must Sign In to use this message board. (secure sign-in)
 
Search this forum  
 FAQ
    Noise  Layout  Per page   
  Refresh
GeneralNeed Full Example Pinmemberpeperkraf18:16 13 Jul '09  
GeneralNPath PinmemberRoger J19:57 6 Jul '06  
GeneralI actualy tried to use this and ... PinmemberPop Catalin5:57 24 Mar '06  
GeneralRe: I actualy tried to use this and ... PinmemberdzCepheus11:15 24 Mar '06  
GeneralRe: I actualy tried to use this and ... PinmemberdzCepheus11:18 24 Mar '06  
GeneralRe: I actualy tried to use this and ... PinmemberAdrian_Moore2:43 28 Mar '06  
GeneralRe: I actualy tried to use this and ... PinmemberPop Catalin4:54 28 Mar '06  
GeneralStrong Typed Alternative PinmemberPop Catalin2:56 24 Mar '06  
GeneralRe: Strong Typed Alternative Pinmember[sharp]3:42 24 Mar '06  
GeneralRe: Strong Typed Alternative PinmemberPop Catalin4:33 24 Mar '06  
GeneralDon't spend too much time on this. PinmemberdzCepheus12:28 23 Mar '06  
GeneralRe: Don't spend too much time on this. Pinmember[sharp]22:52 23 Mar '06  
GeneralRe: Don't spend too much time on this. PinmemberdzCepheus23:03 23 Mar '06  
"you can use Karmencita today."
 
Technically, I can use LINQ today too. They've got an alpha version of C#3 out that works quite nicely in VS 2005 (if you're willing to do without IntelliSense).
 
"following your way of thinking....why should you spend time with LINQ ? You KNOW that after LINQ will be out there will be something that will surpass LINQ ? Why not wait for that ?"
 
LINQ will provide compile-time type checking, full designer support (including IntelliSense), a simpler object model, and with the new features coming with C#3 (lamda expressions, anonymous types, extension methods), there's a lot more sugar to "wait for" that's coming right around the bend.
 
As for waiting for the "next best thing" - when I see it, I'll start waiting. Right now, LINQ is what's on the horizon, and it's *very* close. Worth the wait, in my opinion, especially considering any project I'll be working on will take several months to complete. If I start now with Karmencita, I'll have to change my code to migrate it to LINQ anyway.
 
Perhaps my initial title was too harsh. I didn't mean to imply that Karmencita is not a worthwhile venture, or to offend the author in any way; only that the very problem space Karmencita is being developed to tackle is being built in to the C# compiler with the next release, in a way that none of us really have the access to duplicate. Given that the two are so similar in nature, I have to go with the one that provides designer support and doesn't require string parsing at runtime.
 
I did give this article my 5...
 
PS: This is what part of the alphabet would look like if the letters Q and R were removed.
GeneralRe: Don't spend too much time on this. PinmemberStSz3:20 24 Mar '06  
GeneralRe: Don't spend too much time on this. Pinmember[sharp]3:51 24 Mar '06  
GeneralRe: Don't spend too much time on this. PinmemberdzCepheus11:11 24 Mar '06  
GeneralVery nice... PinmemberMarc Brooks11:28 23 Mar '06  
GeneralRe: Very nice... Pinmember[sharp]3:43 24 Mar '06  

General General    News News    Suggestion Suggestion    Question Question    Bug Bug    Answer Answer    Joke Joke    Rant Rant    Admin Admin   

Use Ctrl+Left/Right to switch messages, Ctrl+Up/Down to switch threads, Ctrl+Shift+Left/Right to switch pages.

Permalink | Advertise | Privacy | Mobile
Web01 | 2.5.120529.1 | Last Updated 23 Mar 2006
Article Copyright 2006 by Marius Gheorghe
Everything else Copyright © CodeProject, 1999-2012
Terms of Use
Layout: fixed | fluid