From 1829b71255e3e08530c654ac213da3f4ae49cf07 Mon Sep 17 00:00:00 2001 From: Jon Hanna Date: Wed, 4 Jan 2017 14:51:49 +0000 Subject: [PATCH] Optimise LinqBridge.Enumerable.Cast for direct cast available. (This changes the behaviour to return same type, but framework Linq has the same optimisation). --- Src/Newtonsoft.Json/Utilities/LinqBridge.cs | 9 ++++++++- 1 file changed, 8 insertions(+), 1 deletion(-) diff --git a/Src/Newtonsoft.Json/Utilities/LinqBridge.cs b/Src/Newtonsoft.Json/Utilities/LinqBridge.cs index f24636f8..b5bdc1ba 100644 --- a/Src/Newtonsoft.Json/Utilities/LinqBridge.cs +++ b/Src/Newtonsoft.Json/Utilities/LinqBridge.cs @@ -72,7 +72,14 @@ namespace Newtonsoft.Json.Utilities.LinqBridge { CheckNotNull(source, "source"); - return CastYield(source); + var servesItself = source as IEnumerable; + if (servesItself != null + && (!(servesItself is TResult[]) || servesItself.GetType().GetElementType() == typeof(TResult))) + { + return servesItself; + } + + return CastYield(source); } private static IEnumerable CastYield(