Skip to content

Commit df2b4f2

Browse files
authored
Remove a couple LINQ usages in Microsoft.Extensions (#47873)
* Remove a couple LINQ usages in Microsoft.Extensions * Respond to PR feedback.
1 parent f0679ab commit df2b4f2

File tree

2 files changed

+16
-6
lines changed

2 files changed

+16
-6
lines changed

src/libraries/Microsoft.Extensions.Configuration.Xml/src/XmlStreamConfigurationProvider.cs

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -68,7 +68,7 @@ public static IDictionary<string, string> Read(Stream stream, XmlDocumentDecrypt
6868
break;
6969

7070
case XmlNodeType.EndElement:
71-
if (prefixStack.Any())
71+
if (prefixStack.Count != 0)
7272
{
7373
// If this EndElement node comes right after an Element node,
7474
// it means there is no text/CDATA node in current element
@@ -179,7 +179,7 @@ private static void AddNamePrefix(XmlReader reader, Stack<string> prefixStack,
179179
}
180180

181181
// If current element is not root element
182-
if (prefixStack.Any())
182+
if (prefixStack.Count != 0)
183183
{
184184
string lastPrefix = prefixStack.Pop();
185185
prefixStack.Push(ConfigurationPath.Combine(lastPrefix, reader.Value));

src/libraries/Microsoft.Extensions.Logging.Abstractions/src/LogValuesFormatter.cs

Lines changed: 14 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,6 @@
44
using System;
55
using System.Collections;
66
using System.Collections.Generic;
7-
using System.Linq;
87
using System.Globalization;
98
using System.Text;
109

@@ -202,10 +201,21 @@ private object FormatArgument(object? value)
202201
}
203202

204203
// if the value implements IEnumerable, build a comma separated string.
205-
var enumerable = value as IEnumerable;
206-
if (enumerable != null)
204+
if (value is IEnumerable enumerable)
207205
{
208-
return string.Join(", ", enumerable.Cast<object>().Select(o => o ?? NullValue));
206+
var vsb = new ValueStringBuilder(stackalloc char[256]);
207+
bool first = true;
208+
foreach (object e in enumerable)
209+
{
210+
if (!first)
211+
{
212+
vsb.Append(", ");
213+
}
214+
215+
vsb.Append(e != null ? e.ToString() : NullValue);
216+
first = false;
217+
}
218+
return vsb.ToString();
209219
}
210220

211221
return value;

0 commit comments

Comments
 (0)