The project implements several useful extensions for the LINQ project. These extensions incude LINQ support for the C++/CLI language and simplified syntax or writing common types of database queries.

CLinq - LINQ support for the C++/CLI language

CLinq project is a library that makes it possible to use LINQ technologies from the C++/CLI language.

The LINQ project which will be part of the next version of Visual Studio (codename "Orcas") is set of extensions that make it possible to query data sources directly from the C# or VB.NET languages. LINQ extends .NET Framework with classes to represent queries and both C# and VB.NET language with features that make it possible to write these queries easily. It also includes libraries for using queries with the most common types of data sources like SQL database, DataSets and XML files. This article requires some basic knowledge of LINQ and C# 3.0, so I recommend looking at the LINQ Overview available from the official project web site before reading the article.

LINQ includes extensions for the C# and VB.NET, but there are no plans for supporting LINQ in C++/CLI. The goal of CLinq project is to allow using part of LINQ functionality from C++/CLI. Thanks to very powerful operator overloading mechanism in C++/CLI it is possible to enable using LINQ to SQL for accessing SQL databases in C++/CLI as well as some other LINQ uses.

I will first demonstrate how the same database query looks in C# 3.0 and C++/CLI and then we will look at CLinq in more detail. The following query (written in C# 3.0) uses the Northwind database and returns name of contact and company for all customers living in London:

// create connection to database
NorthwindData db = new NorthwindData(".. connection string ..");

// declare database query
var q = 
  from cvar in db.Customers
  where cvar.City == "London"
  select cvar.ContactName + ", " + cvar.CompanyName;

// execute query and output results
foreach(string s in q)
  Console.WriteLine(s);


Now, let's look at the same query written in C++/CLI using CLinq. It is a bit more complex, but this is the price for implementing it as a library instead of modifying the language:

// create connection to database
NorthwindData db(".. connection string ..");

// declare database query
Expr<Customers^> cvar = Var<Customers^>("c");
CQuery<String^>^ q = db.QCustomers
  ->Where(clq::fun(cvar, cvar.City == "London"))
  ->Select(clq::fun(cvar, 
      cvar.ContactName + Expr<String^>(", ") + cvar.CompanyName));

// execute query and output results
for each(String^ s in q->Query)
  Console::WriteLine(s);

Other LINQ extensions

I'm planing to add a few other extensions that I'm working on in the future. If you're interested in these extensions, you can look at my blog:

Comments & suggestions

I'm interested in your comments & suggestions! Let me know at tomas@tomasp.net

Last edited Mar 3, 2007 at 5:10 PM by tomasp, version 4