From 7cc8ce2b2020caa613669312cef1b0acac470ace Mon Sep 17 00:00:00 2001 From: Maurycy Markowski Date: Thu, 5 Jan 2017 14:38:03 -0800 Subject: [PATCH] Fix to #7364 - Query : ToString() method that takes a formatter argument should still be translated on the client Fix is to only translate ToString calls that have no arguments. Ones with the arguments will be evaluated on the client. --- .../QueryTestBase.cs | 19 +++++++++++++++++++ .../SqlServerObjectToStringTranslator.cs | 2 ++ .../QuerySqlServerTest.cs | 15 +++++++++++++++ 3 files changed, 36 insertions(+) diff --git a/src/Microsoft.EntityFrameworkCore.Specification.Tests/QueryTestBase.cs b/src/Microsoft.EntityFrameworkCore.Specification.Tests/QueryTestBase.cs index d1893bf891d..486060f4027 100644 --- a/src/Microsoft.EntityFrameworkCore.Specification.Tests/QueryTestBase.cs +++ b/src/Microsoft.EntityFrameworkCore.Specification.Tests/QueryTestBase.cs @@ -3,6 +3,7 @@ using System; using System.Collections.Generic; +using System.Globalization; using System.Linq; using System.Linq.Expressions; using Microsoft.EntityFrameworkCore.Infrastructure; @@ -6437,6 +6438,24 @@ public virtual void Select_expression_int_to_string() })); } + [ConditionalFact] + public virtual void ToString_with_formatter_is_evaluated_on_the_client() + { + AssertQuery( + os => os.Where(o => o.OrderDate != null) + .Select(o => new Order + { + ShipName = o.OrderID.ToString("X") + })); + + AssertQuery( + os => os.Where(o => o.OrderDate != null) + .Select(o => new Order + { + ShipName = o.OrderID.ToString(new CultureInfo("en-US")) + })); + } + [ConditionalFact] public virtual void Select_expression_date_add_year() { diff --git a/src/Microsoft.EntityFrameworkCore.SqlServer/Query/ExpressionTranslators/Internal/SqlServerObjectToStringTranslator.cs b/src/Microsoft.EntityFrameworkCore.SqlServer/Query/ExpressionTranslators/Internal/SqlServerObjectToStringTranslator.cs index 16e26ae5e8f..b6266064077 100644 --- a/src/Microsoft.EntityFrameworkCore.SqlServer/Query/ExpressionTranslators/Internal/SqlServerObjectToStringTranslator.cs +++ b/src/Microsoft.EntityFrameworkCore.SqlServer/Query/ExpressionTranslators/Internal/SqlServerObjectToStringTranslator.cs @@ -4,6 +4,7 @@ using System; using System.Collections.Generic; using System.Linq.Expressions; +using System.Reflection; using Microsoft.EntityFrameworkCore.Query.Expressions; namespace Microsoft.EntityFrameworkCore.Query.ExpressionTranslators.Internal @@ -48,6 +49,7 @@ public virtual Expression Translate(MethodCallExpression methodCallExpression) string storeType; if (methodCallExpression.Method.Name == nameof(ToString) + && methodCallExpression.Arguments.Count == 0 && methodCallExpression.Object != null && _typeMapping.TryGetValue( methodCallExpression.Object.Type diff --git a/test/Microsoft.EntityFrameworkCore.SqlServer.FunctionalTests/QuerySqlServerTest.cs b/test/Microsoft.EntityFrameworkCore.SqlServer.FunctionalTests/QuerySqlServerTest.cs index f0f5376ba10..f4dba756a5a 100644 --- a/test/Microsoft.EntityFrameworkCore.SqlServer.FunctionalTests/QuerySqlServerTest.cs +++ b/test/Microsoft.EntityFrameworkCore.SqlServer.FunctionalTests/QuerySqlServerTest.cs @@ -6355,6 +6355,21 @@ FROM [Orders] AS [o] Sql); } + public override void ToString_with_formatter_is_evaluated_on_the_client() + { + base.ToString_with_formatter_is_evaluated_on_the_client(); + + Assert.Equal( + @"SELECT [o].[OrderID] +FROM [Orders] AS [o] +WHERE [o].[OrderDate] IS NOT NULL + +SELECT [o].[OrderID] +FROM [Orders] AS [o] +WHERE [o].[OrderDate] IS NOT NULL", + Sql); + } + public override void Select_expression_other_to_string() { base.Select_expression_other_to_string();