Is There a Linq Library for C++

Is there an equivalent in C++ to LINQ with respect to DataTables?

Look at this
linq-in-c
but, imho, best way to use boolinq

for example

struct Man
{
std::string name;
int age;
};

Man src[] =
{
{"Kevin",14},
{"Anton",18},
{"Agata",17},
{"Terra",20},
{"Layer",15},
};

auto dst = from(src).where( [](const Man & man){return man.age < 18;})
.orderBy([](const Man & man){return man.age;})
.select( [](const Man & man){return man.name;})
.toVector();

C# Linq expression in C++

The C++ community seems to be moving towards non-member functions for this type of "utility thing not directly related to the class at hand". e.g. if you have a std::vector v; you could always v.begin(), but now you can also std::begin(v). This would imply something like select(theVector)....

But, you might want to support joins at some point, so I would consider

select(...).from(theVector).join(theMap, ...).where(...)
// or
select(...).from(theVector).order_by(...)
// etc.

How to use LINQ in C++/CLI - in VS 2010/.Net 4.0

You can use the Linq methods that are defined in the System::Linq namespace, but you'll have to jump through a couple extra hoops.

First, C++/CLI doesn't support extension methods. However, the extension methods are regular methods defined on various classes in System::Linq, so you can call them directly.

List<int>^ list = gcnew List<int>();
int i = Enumerable::FirstOrDefault(list);

Second, C++/CLI doesn't support lambda expressions. The only workaround is to declare an actual method, and pass that as a delegate.

ref class Foo
{
public:
static bool GreaterThanZero(int i) { return i > 0; }

void Bar()
{
List<int>^ list = gcnew List<int>();
int i = Enumerable::FirstOrDefault(list, gcnew Func<int, bool>(&Foo::GreaterThanZero));
}
}

Is there a Linq to REST library for C#

I did find one eventually in Linq2Rest (also a NuGet) which seems to fit the bill. Doesn't support OAuth but would be possible to build this in.

C++ LINQ-like iterator operations

I don't have concrete experience with LINQ, but the Boost.Iterator library seems to approach what you're referring to.

The idea is to have functions (IIUC, in LINQ, they take the form of extension methods, but that's not fundamental), taking an iterator and a function, combining them to create a new iterator.

LINQ "Where" maps to make_filter_iterator:

std::vector<int> vec = ...;
// An iterator skipping values less than "2":
boost::make_filter_iterator(_1 > 2, vec.begin())

LINQ "Select" maps to make_transform_iterator:

using namespace boost::lambda;
//An iterator over strings of length corresponding to the value
//of each element in "vec"
//For example, 2 yields "**", 3 "***" and so on.
boost::make_transform_iterator(construct<std::string>('*', _1), vec.begin())

And they can be composed:

//An iterator over strings of length corresponding to the value of each element
// in "vec", excluding those less than 2
std::vector<int> vec = ...;
boost::make_transform_iterator(construct<std::string>('*', _1),
boost::make_filter_iterator(_1 > 2, vec.begin())
)

However, there are a few annoying things with this:

  • The type returned by make_xxx_iterator(some_functor, some_other_iterator) is xxx_iterator<type_of_some_functor, type_of_some_iterator>
  • The type of a functor created using boost::bind, lambda, or phoenix quickly becomes unmanageably large and cumbersome to write.

That's why I avoided in the code above to assign the result of make_xxx_iterator to a variable. C++0x "auto" feature will be pretty welcome there.

But still, a C++ iterator can't live "alone": they have to come in pairs to be useful. So, even with "auto", it's still a mouthful:

auto begin = make_transform_iterator(construct<std::string>('*', _1), 
make_filter_iterator(_1 > 2, vec.begin())
);
auto end = make_transform_iterator(construct<std::string>('*', _1),
make_filter_iterator(_1 > 2, vec.end())
);

Avoiding the use of lambda makes things verbose, but manageable:

struct MakeStringOf{
MakeStringOf(char C) : m_C(C){}
char m_C;

std::string operator()(int i){return std::string(m_C, i);}
};

struct IsGreaterThan{
IsGreaterThan(int I) : m_I(I){}
int m_I;

bool operator()(int i){return i > m_I;}
};

typedef boost::filter_iterator<
IsGreaterThan,
std::vector<int>::iterator
> filtered;

typedef boost::transform_iterator<
MakeStringOf,
filtered
> filtered_and_transformed;

filtered_and_transformed begin(
MakeStringOf('*'),
filtered(IsGreaterThan(2), vec.begin())
);

filtered_and_transformed end(
MakeStringOf('*'),
filtered(IsGreaterThan(2), vec.end())
);

The (not-yet)Boost.RangeEx library is promising in this respect, in that it allows to combine the two iterators in a single range. Something like:

auto filtered_and_transformed = make_transform_range(
make_filter_range(vec, _1 > 2),
construct<std::string>('*', _1)
);

Is there something in PHP equivalent to LINQ in C#?

There is PHPLinq - LINQ for PHP.



Related Topics



Leave a reply



Submit