Convert 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.

Advertisements
This entry was posted in .NET, c#, Roslyn and tagged , , , , , , . Bookmark the permalink.

1 Response to Convert linq query syntax to fluent syntax with Roslyn

  1. Pingback: When to use C# LINQ with Query Syntax over Method Syntax - Michael's Coding Spot

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 )

Google photo

You are commenting using your Google 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 )

Connecting to %s

This site uses Akismet to reduce spam. Learn how your comment data is processed.