CLR internals, Roslyn

Converting LINQ query syntax to fluent syntax with Roslyn

In c# you can write LINQ queries in two ways. One is the fluent invocation syntax (extension method) and the other is query comprehension syntax.

Fluent example:

Enumerable.Range(1, 5).
Where(i => i % 2 == 0).
Select(i => i * 2);

Comprehension example:

from i in Enumerable.Range(1, 5)
where i % 2 == 0
select i * 2;

I was needed the ability to take the comprehension syntax and convert it to fluent syntax. So I wrote a rewriter with Roslyn that do exactly this.

It’s not complete implementation (its not do any syntax validation checks), but its good point to start with.

You can find it in GitHub

If you test it and you find a bug, please let me know.

Here you can find a very helpful query expression translation cheat sheet.