Click here to Skip to main content
15,885,546 members
Articles / Programming Languages / C#
Article

Karmencita: an object query language for .NET

Rate me:
Please Sign up or sign in to vote.
4.42/5 (16 votes)
23 Mar 2006 68.3K   208   27   18
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:
    C#
    // 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:
    C#
    // 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:
    C#
    // 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:
    C#
    // 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


Written By
United States United States
This member has not yet provided a Biography. Assume it's interesting and varied, and probably something to do with programming.

Comments and Discussions

 
GeneralNeed Full Example Pin
peperkraf13-Jul-09 18:16
peperkraf13-Jul-09 18:16 
GeneralNPath Pin
Roger Alsing6-Jul-06 19:57
Roger Alsing6-Jul-06 19:57 
GeneralI actualy tried to use this and ... Pin
Pop Catalin24-Mar-06 5:57
Pop Catalin24-Mar-06 5:57 
GeneralRe: I actualy tried to use this and ... Pin
dzCepheus24-Mar-06 11:15
dzCepheus24-Mar-06 11:15 
GeneralRe: I actualy tried to use this and ... Pin
dzCepheus24-Mar-06 11:18
dzCepheus24-Mar-06 11:18 
GeneralRe: I actualy tried to use this and ... Pin
Adrian_Moore28-Mar-06 2:43
Adrian_Moore28-Mar-06 2:43 
GeneralRe: I actualy tried to use this and ... Pin
Pop Catalin28-Mar-06 4:54
Pop Catalin28-Mar-06 4:54 
GeneralStrong Typed Alternative Pin
Pop Catalin24-Mar-06 2:56
Pop Catalin24-Mar-06 2:56 
GeneralRe: Strong Typed Alternative Pin
[sharp]24-Mar-06 3:42
[sharp]24-Mar-06 3:42 
GeneralRe: Strong Typed Alternative Pin
Pop Catalin24-Mar-06 4:33
Pop Catalin24-Mar-06 4:33 
GeneralDon't spend too much time on this. Pin
dzCepheus23-Mar-06 12:28
dzCepheus23-Mar-06 12:28 
GeneralRe: Don't spend too much time on this. Pin
[sharp]23-Mar-06 22:52
[sharp]23-Mar-06 22:52 
GeneralRe: Don't spend too much time on this. Pin
dzCepheus23-Mar-06 23:03
dzCepheus23-Mar-06 23:03 
GeneralRe: Don't spend too much time on this. Pin
StSz24-Mar-06 3:20
StSz24-Mar-06 3:20 
GeneralRe: Don't spend too much time on this. Pin
[sharp]24-Mar-06 3:51
[sharp]24-Mar-06 3:51 
GeneralRe: Don't spend too much time on this. Pin
dzCepheus24-Mar-06 11:11
dzCepheus24-Mar-06 11:11 
"You will NEVER know when Linq is available."

-- LINQ will be available publicly when they release Orcas, which will be Q4 this year (so I've heard from various MSFT blogs). Failing that, I'm pretty sure I will know when it's available when it becomes available... :P



"Karmencita is easier to use and needs less overhead"

-- Your first point is opinion, the second I'd like to see backed up by some facts... From what I've seen, LINQ is largely syntactic in nature -- that is, LINQ queries are, when compiled, translated into normal .NET calls on Static extension methods. Karmencita has the runtime requirement of string parsing on calls (not sure how/if Karmencita does any query caching), which adds considerable runtime overhead.



"Karmencita uses strings for queries and thus is more dynamic (but also more error prone and has more security risks)"

-- True, Karmencita is more dynamic than LINQ. If you need a dynamic data query language, that end-users will be writing queries with, then I would definately recommend Karmencita. That's why I gave it a 5 rating in the first place - it solves a problem that LINQ can't. Though as you say, with that flexibility comes security risks - as well as performance issues at runtime with string parsing.



"With some effort you can build some sort of ADO.NET layer around Karmencita, since it uses pseudo-SQL, and use it for some sort of in-memory databases."

-- But LINQ already does this. It's called DLINQ. They also have one for XML called XLINQ. I wouldn't have to expend any effort to use it, and I could start building for it literally today with the preview compiler. Granted I can't use it in production, but that isn't an issue for me because I'm using it with new products we're just beginning to develop.

Also, one of the reasons they're making LINQ is to tie data access in to the C# and VB languages as a first-class being. One of the historic problems with data access currently is that, when you litter your code with SQL command strings for example, if your database schema changes you have to walk through your entire codebase looking for whichever SQL commands you have to change. How does Karmencia help with this, when it too is string-based?



"As you see, Linq and Karmencita are just two different things. Neither is the "best""

-- I never said one was *best*; just that the two systems are extremely similar and lie in the same problem space. One has compile-time support, which results in faster execution, type safety, compile-time checking, language integration, etc. but lacks a dynamic runtime extension model, while the other is dynamically editable at runtime but must be string-parsed with no compile-time support at all.

There are pros and cons to both - I just want people to be aware that there is another solution coming on the horizon.

PS: This is what part of the alphabet would look like if the letters Q and R were removed.
GeneralVery nice... Pin
Marc Brooks23-Mar-06 11:28
Marc Brooks23-Mar-06 11:28 
GeneralRe: Very nice... Pin
[sharp]24-Mar-06 3:43
[sharp]24-Mar-06 3:43 

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

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