Posted by: Zeeshan Amjad | May 24, 2011

Use LINQ Standard Query Operator in VC++


LINQ is a very powerful technique Microsoft introduced in C# 3.0. While writing program in C# 3.0 or above using LINQ we can use two different types of approached query syntax or method syntax. Although query syntax is very declarative and easy to understand, but for .Net runtime environment (CLR) it has to convert into method syntax. These methods are called “Standard Query Operator”.

Query syntax is a syntactical sugar in C# compiler and we can’t do it in VC++ (at least not easily). But we can take an advantage of LINQ in VC++ by using the Standard Query Operator or in other words method call.

Let’s first take a look at example in C# that uses query syntax.

Code Snippet
int[] data = (from n in Enumerable.Range(0, 100)
               where n < 50 && n > 10
               orderby n descending
               select n).ToArray();

foreach (int item in data)
{
    Console.WriteLine(item);
}

 

Here we are going to create an array of elements greater than 10 and less than 50 in descending order and then print its values. This is LINQ using query syntax. Now we are going to do the same thing but with method syntax. Here is updated version of this program.

Code Snippet
int[] data = Enumerable.Range(0, 100).
    Where(n => n < 50 && n > 10).OrderByDescending(n => n).
    ToArray();

foreach (int item in data)
{
    Console.WriteLine(item);
}

 

In this program we are using the anonymous method and extension method. Now let’s do the same thing without extension method. Here is updated version of this program.

Code Snippet
IEnumerable<int> range = Enumerable.Range(0, 100);
range = range.Where(n => n < 50 && n > 10);
range = range.OrderByDescending(n => n);
int[] data = range.ToArray();

foreach (int item in data)
{
    Console.WriteLine(item);
}

 

This program still use anonymous method. Now lets use the named method. First we create two methods, one for filtering and other one for sorting. Here is our methods.

Code Snippet
static bool FilterPredicate(int n)
{
    return n < 50 && n > 10 ? true : false;
}

static int Sorting(int n)
{
    return n;
}

 

Now we use these methods in our standard query operator. Here is updated version of the program.

Code Snippet
IEnumerable<int> range = Enumerable.Range(0, 100);
range = range.Where(FilterPredicate);
range = range.OrderByDescending(Sorting);
int[] data = range.ToArray();

foreach (int item in data)
{
    Console.WriteLine(item);
}

 

The output of all of the programs are same. Now lets make the same program (last one) in VC++. Here is our function written in VC++ used in query operator.

Code Snippet
bool FilterPredicate(int n)
{
    return n < 50 && n > 10 ? true : false;
}

int Sorting(int n)
{
    return n;
}

 

Here is our VC++ version of the same program .

Code Snippet
IEnumerable<int>^ range = Enumerable::Range(0, 100);
range = Enumerable::Where(range, gcnew Func<int, bool>(&FilterPredicate));
range = Enumerable::OrderByDescending(range, gcnew Func<int, int>(&Sorting));
array<int>^ data = Enumerable::ToArray(range);

int length = data->Length;

for (int i = 0; i < length; i++)
{
    Console::WriteLine(data[i]);
}

 

Here is our complete VC++ console based program to show the usage of standard query operator.

Code Snippet
// LinqVC.cpp : main project file.

#include "stdafx.h"

using namespace System;
using namespace System::Collections::Generic;
using namespace System::Linq;

bool FilterPredicate(int n)
{
    return n < 50 && n > 10 ? true : false;
}

int Sorting(int n)
{
    return n;
}

int main(array<System::String ^> ^args)
{
    IEnumerable<int>^ range = Enumerable::Range(0, 100);
    range = Enumerable::Where(range, gcnew Func<int, bool>(&FilterPredicate));
    range = Enumerable::OrderByDescending(range, gcnew Func<int, int>(&Sorting));
    array<int>^ data = Enumerable::ToArray(range);
    
    int length = data->Length;

    for (int i = 0; i < length; i++)
    {
        Console::WriteLine(data[i]);
    }

    return 0;
}

Advertisements

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s

Categories

%d bloggers like this: