7

I search for a VS2010 compatable C++ linq library with C# LINQ dot sintax. meaning something like: from(...).where(...).orderBy.firstOrDefault();

I googled and found this so answer LINQ libraries collection/mess:

Others I found not using dot notation.. btw pfultz2/Linq seems to provide orderBy and first yet its SQL like LINQ sintax and Limitations make it something I am not looking for=(

So Is ther any opensource C++ LINQ library with dot notation, orderBy and firstOrDefault?

Community
  • 1
  • 1
myWallJSON
  • 9,110
  • 22
  • 78
  • 149
  • The term "stable" sort [has a special meaning](http://en.wikipedia.org/wiki/Sorting_algorithm#Stability), which I don't think you mean here - you should rephrase. – Eamon Nerbonne Mar 16 '13 at 20:50
  • @EamonNerbonne: rephrased. – myWallJSON Mar 16 '13 at 21:02
  • If there is none that meets all your needs. It is time to start hacking. You might either enhance one of the strongest of them, or start your own. – Lex Li Mar 17 '13 at 02:09
  • 1
    Actually [pfultz2/Linq](https://github.com/pfultz2/Linq) does support `order_by` and `first` as extension methods: `auto q = people | linq::order_by([](person p){ return p.name; }) | linq::first;`. If you want to fix the compiler errors for msvc, that would be great. Or you can always change to a better compiler. – Paul Fultz II Mar 18 '13 at 02:24
  • 2
    My library **linq-cpp** has pretty much all of the .NET LINQ functions available: https://github.com/timothy-shields/linq-cpp/tree/v1 (link is for version 1 of the project) - Version 2 (the current HEAD of master branch) has loads of improvements but isn't finished yet. At the very least, check out version 1. :) - If you're interested in this kind of project, contact me! I'm looking for collaborators to help me really polish it off and make it the best C++ LINQ library available. – Timothy Shields Jun 17 '13 at 04:47
  • Forgot to mention - **linq-cpp** of course has dot notation, OrderBy, and FirstOrDefault - I wouldn't be mentioning it otherwise. :) – Timothy Shields Jun 17 '13 at 04:51

1 Answers1

1

Well, I will not give you the reply you want, but it will be a reply anyway :-)

LINQ is thought for C# mainly. I think your use case should be for translating C# code into C++, but I think that the effective way in C++ is to use Boost.Range.

Boost.Range reuses the c++ standard library in a way that is easy to do query on data:

  1. You can use adaptors for containers with left to right notation using operator |. They are evaluated lazily, just as in LINQ.
  2. You can perform operations such as std::min, std::max, std::all_of, std::any_of, std::none_of in the adapted ranges.

An example I wrote the other day is how to reverse words in a string. The solution was something like this:

using string_range = boost::iterator_range<std::string::const_iterator>;

struct submatch_to_string_range {
    using result_type = string_range;

    template <class T>
    string_range operator()(T const & s) const {
        return string_range(s.first, s.second);

    }
};

string sentence = "This is a sentence";

auto words_query = sentence |
                ba::tokenized(R"((\w+))") |
                ba::transformed(submatch_to_string_range{}) |
                ba::reversed;         


vector<string_range> words(words_query.begin(), words_query.end());

for (auto const & w : words) 
cout << words << endl;

I highly recommend you to base your queries upon this library, since this is going to be supported for a very long time and I think it will be the future. You can do same style of queries.

It would be nice if this library could be extended with things like | max and | to_vector to avoid naming the vector directly and copying, but I think that as a query language, right now, it's more than acceptable.

Germán Diago
  • 7,473
  • 1
  • 36
  • 59