Browse Source

Do not convert simple LINQ method calls to LINQ expressions. Closes #1501.

pull/1633/head
Siegfried Pammer 6 years ago
parent
commit
fa7c1f574a
  1. 16
      ICSharpCode.Decompiler.Tests/TestCases/Pretty/ExpressionTrees.cs
  2. 4
      ICSharpCode.Decompiler.Tests/TestCases/Pretty/InitializerTests.cs
  3. 14
      ICSharpCode.Decompiler/CSharp/Transforms/IntroduceQueryExpressions.cs

16
ICSharpCode.Decompiler.Tests/TestCases/Pretty/ExpressionTrees.cs

@ -625,16 +625,15 @@ namespace ICSharpCode.Decompiler.Tests.TestCases.Pretty
//no params //no params
ToCode(X(), () => call(() => 42)); ToCode(X(), () => call(() => 42));
//one param //one param
ToCode(X(), () => from x in new int[2] { ToCode(X(), () => new int[2] {
37, 37,
42 42
} }.Select((int x) => x * 2));
select x * 2);
//two params //two params
ToCode(X(), () => new int[2] { ToCode(X(), () => new int[2] {
37, 37,
42 42
}.Select((int x, int i) => x * 2)); }.Select((int x, int i) => x * 2));
} }
public void CurriedLambda() public void CurriedLambda()
@ -731,13 +730,12 @@ namespace ICSharpCode.Decompiler.Tests.TestCases.Pretty
public void QuotedWithAnonymous() public void QuotedWithAnonymous()
{ {
ToCode(X(), () => (from o in new[] { ToCode(X(), () => new[] {
new { new {
X = "a", X = "a",
Y = "b" Y = "b"
} }
} }.Select(o => o.X + o.Y).Single());
select o.X + o.Y).Single());
} }
public void StaticCall() public void StaticCall()

4
ICSharpCode.Decompiler.Tests/TestCases/Pretty/InitializerTests.cs

@ -1468,9 +1468,7 @@ namespace ICSharpCode.Decompiler.Tests.TestCases.Pretty.InitializerTests
DateTimeFormat = new DateTimeFormatInfo { DateTimeFormat = new DateTimeFormatInfo {
ShortDatePattern = "ddmmyy" ShortDatePattern = "ddmmyy"
}, },
NumberFormat = (from format in source NumberFormat = source.Where((NumberFormatInfo format) => format.CurrencySymbol == "$").First()
where format.CurrencySymbol == "$"
select format).First()
} }
}); });
} }

14
ICSharpCode.Decompiler/CSharp/Transforms/IntroduceQueryExpressions.cs

@ -90,10 +90,11 @@ namespace ICSharpCode.Decompiler.CSharp.Transforms
if (mre == null || IsNullConditional(mre.Target)) if (mre == null || IsNullConditional(mre.Target))
return null; return null;
switch (mre.MemberName) { switch (mre.MemberName) {
case "Select": case "Select": {
{
if (invocation.Arguments.Count != 1) if (invocation.Arguments.Count != 1)
return null; return null;
if (!IsComplexQuery(mre))
return null;
ParameterDeclaration parameter; ParameterDeclaration parameter;
Expression body; Expression body;
if (MatchSimpleLambda(invocation.Arguments.Single(), out parameter, out body)) { if (MatchSimpleLambda(invocation.Arguments.Single(), out parameter, out body)) {
@ -158,6 +159,8 @@ namespace ICSharpCode.Decompiler.CSharp.Transforms
{ {
if (invocation.Arguments.Count != 1) if (invocation.Arguments.Count != 1)
return null; return null;
if (!IsComplexQuery(mre))
return null;
ParameterDeclaration parameter; ParameterDeclaration parameter;
Expression body; Expression body;
if (MatchSimpleLambda(invocation.Arguments.Single(), out parameter, out body)) { if (MatchSimpleLambda(invocation.Arguments.Single(), out parameter, out body)) {
@ -175,6 +178,8 @@ namespace ICSharpCode.Decompiler.CSharp.Transforms
{ {
if (invocation.Arguments.Count != 1) if (invocation.Arguments.Count != 1)
return null; return null;
if (!IsComplexQuery(mre))
return null;
ParameterDeclaration parameter; ParameterDeclaration parameter;
Expression orderExpression; Expression orderExpression;
if (MatchSimpleLambda(invocation.Arguments.Single(), out parameter, out orderExpression)) { if (MatchSimpleLambda(invocation.Arguments.Single(), out parameter, out orderExpression)) {
@ -250,6 +255,11 @@ namespace ICSharpCode.Decompiler.CSharp.Transforms
} }
} }
static bool IsComplexQuery(MemberReferenceExpression mre)
{
return ((mre.Target is InvocationExpression && mre.Parent is InvocationExpression) || mre.Parent?.Parent is QueryClause);
}
QueryFromClause MakeFromClause(ParameterDeclaration parameter, Expression body) QueryFromClause MakeFromClause(ParameterDeclaration parameter, Expression body)
{ {
QueryFromClause fromClause = new QueryFromClause { QueryFromClause fromClause = new QueryFromClause {

Loading…
Cancel
Save