Skip to content

Commit

Permalink
VB -> C#: Convert orderby distinct in linq correctly - fixes #635
Browse files Browse the repository at this point in the history
  • Loading branch information
GrahamTheCoder committed Jul 5, 2021
1 parent 8278d9d commit e98a632
Show file tree
Hide file tree
Showing 3 changed files with 31 additions and 2 deletions.
1 change: 1 addition & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,7 @@ The format is based on [Keep a Changelog](https://keepachangelog.com/en/1.0.0/)

### VB -> C#

* Convert `orderby distinct` in linq - fixes #635

### C# -> VB

Expand Down
4 changes: 2 additions & 2 deletions CodeConverter/CSharp/QueryConverter.cs
Original file line number Diff line number Diff line change
Expand Up @@ -93,12 +93,12 @@ public async Task<CSharpSyntaxNode> ConvertClausesAsync(SyntaxList<VBSyntax.Quer
new Queue<(SyntaxList<CSSyntax.QueryClauseSyntax>, VBSyntax.QueryClauseSyntax)>();
while (vbBodyClauses.Any() && !RequiresMethodInvocation(vbBodyClauses.Peek())) {
var convertedClauses = new List<CSSyntax.QueryClauseSyntax>();
while (vbBodyClauses.Any() && !RequiredContinuation(vbBodyClauses.Peek(), vbBodyClauses.Count - 1)) {
while (vbBodyClauses.Any() && !RequiredContinuation(vbBodyClauses.Peek(), vbBodyClauses.Count - 1) && !RequiresMethodInvocation(vbBodyClauses.Peek())) {
convertedClauses.Add(await ConvertQueryBodyClauseAsync(vbBodyClauses.Dequeue()));
}

var convertQueryBodyClauses = (SyntaxFactory.List(convertedClauses),
vbBodyClauses.Any() ? vbBodyClauses.Dequeue() : null);
vbBodyClauses.Any() && !RequiresMethodInvocation(vbBodyClauses.Peek()) ? vbBodyClauses.Dequeue() : null);
querySectionsReversed.Enqueue(convertQueryBodyClauses);
}
querySegments.Add((querySectionsReversed, vbBodyClauses.Any() ? vbBodyClauses.Dequeue() : null));
Expand Down
28 changes: 28 additions & 0 deletions Tests/CSharp/ExpressionTests/LinqExpressionTests.cs
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,34 @@ namespace ICSharpCode.CodeConverter.Tests.CSharp.ExpressionTests
{
public class LinqExpressionTests : ConverterTestBase
{
[Fact]
public async Task Issue635_LinqDistinctOrderByAsync()
{
await TestConversionVisualBasicToCSharpAsync(@"
Imports System.Collections.Generic
Imports System.Linq
Public Class Issue635
Dim l As List(Of Integer)
Dim listSortedDistinct = From x In l Order By x Distinct
End Class",
@"using System.Collections.Generic;
using System.Linq;
public partial class Issue635
{
public Issue635()
{
listSortedDistinct = (from x in l
orderby x
select x).Distinct();
}
private List<int> l;
private object listSortedDistinct;
}");
}

[Fact]
public async Task Linq1Async()
{
Expand Down

0 comments on commit e98a632

Please sign in to comment.