diff --git a/CodeConverter/CSharp/LambdaConverter.cs b/CodeConverter/CSharp/LambdaConverter.cs index f1306f5b..4a90398a 100644 --- a/CodeConverter/CSharp/LambdaConverter.cs +++ b/CodeConverter/CSharp/LambdaConverter.cs @@ -79,8 +79,17 @@ public async Task ConvertAsync(VBSyntax.LambdaExpressionSyntax convertedStatements = convertedStatements.Select(l => l.WithTrailingTrivia(SyntaxFactory.ElasticCarriageReturnLineFeed)).ToList(); block = SyntaxFactory.Block(convertedStatements); } else if (singleStatement.TryUnpackSingleExpressionFromStatement(out expressionBody)) { - arrow = SyntaxFactory.ArrowExpressionClause(expressionBody); + if (vbNode is VBasic.Syntax.MultiLineLambdaExpressionSyntax && singleStatement is not ExpressionStatementSyntax && singleStatement is not ReturnStatementSyntax) { + singleStatement = singleStatement.WithTrailingTrivia(SyntaxFactory.ElasticCarriageReturnLineFeed); + block = SyntaxFactory.Block(singleStatement); + expressionBody = null; + } else { + arrow = SyntaxFactory.ArrowExpressionClause(expressionBody); + } } else { + if (vbNode is VBasic.Syntax.MultiLineLambdaExpressionSyntax) { + singleStatement = singleStatement.WithTrailingTrivia(SyntaxFactory.ElasticCarriageReturnLineFeed); + } block = SyntaxFactory.Block(singleStatement); } diff --git a/Tests/CSharp/ExpressionTests/LambdaTests.cs b/Tests/CSharp/ExpressionTests/LambdaTests.cs new file mode 100644 index 00000000..09954bed --- /dev/null +++ b/Tests/CSharp/ExpressionTests/LambdaTests.cs @@ -0,0 +1,56 @@ +using System.Threading.Tasks; +using ICSharpCode.CodeConverter.Tests.TestRunners; +using Xunit; + +namespace ICSharpCode.CodeConverter.Tests.CSharp.ExpressionTests; + +public class LambdaTests : ConverterTestBase +{ + [Fact] + public async Task Issue1012_MultiLineLambdaWithSingleStatement() + { + await TestConversionVisualBasicToCSharpAsync(@"Imports System.Collections.Generic +Imports System.Linq +Imports System.Threading.Tasks + +Public Class ConversionTest3 + Private Class MyEntity + Property EntityId As Integer + Property Name As String + End Class + Private Sub BugRepro() + + Dim entities As New List(Of MyEntity) + + Parallel.For(1, 3, Sub(i As Integer) + Dim result As String = (From e In entities + Where e.EntityId = 123 + Select e.Name).Single + End Sub) + End Sub +End Class", @"using System.Collections.Generic; +using System.Linq; +using System.Threading.Tasks; + +public partial class ConversionTest3 +{ + private partial class MyEntity + { + public int EntityId { get; set; } + public string Name { get; set; } + } + private void BugRepro() + { + + var entities = new List(); + + Parallel.For(1, 3, (i) => + { + string result = (from e in entities + where e.EntityId == 123 + select e.Name).Single(); + }); + } +}"); + } +}