Skip to content
hjiang edited this page Sep 13, 2010 · 11 revisions

Linq++

Introduction

This is a simple experimental implementation of a LINQ-like query language for C++ containers. I’ve just started working on it, so it’s still just a toy. Linq++ depends on the Boost C++ library.

Suppose you have an STL container:


struct Person { string name; int age; Person(const string& aName, int anAge) : name(aName), age(anAge) {} }; shared_ptr<vector<Person> > guests;

You can do simple things like:
Count the number of people older than 30


cout << from(guests) .where(&_1 ->* &Person::age > 30) .count()

Find people older than 30 and create a vector with their ages

from(guests) .where(&_1 ->* &Person::age > 30) .select<int>(&_1 ->* &Person::age);

A snippet from the unit tests illustrates its expressivenss:


// combine the people older than 30 with the person with name "joe" into one table. DataSet<vector<Person> > results = insert( from(guests) .where(&_1 ->* &Person::age > 30)) .into( from(guests) .where(&_1 ->* &Person::name == "joe"));

// select the age column from the previous table.
shared_ptr<vector > ages = results
.select(&_1 →* &Person::age)
.get();



See the companion unit tests for details.
Clone this wiki locally