You cannot select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
OMS.NET/Common/JsonArrayExtensions.cs

31 lines
929 B
C#

using System.Text.Json;
using System.Text.Json.Nodes;
namespace OMS.NET.Common
{
public static class Extensions
{
public static int FindElementIndex(this JsonArray jsonArray, long elementToFind)
{
for (int i = 0; i < jsonArray.Count; i++)
{
JsonNode node = jsonArray.ElementAt(i)!;
if (node.GetValueKind() == JsonValueKind.Number &&
node.GetValue<long>() == elementToFind)
{
return i;
}
}
return -1; // 未找到目标元素,返回 -1
}
public static void RemoveAll(this JsonArray jsonArray, Func<JsonNode?, bool> predicate)
{
var itemsToRemove = jsonArray.Where(predicate).ToList();
foreach (var item in itemsToRemove)
{
jsonArray.Remove(item);
}
}
}
}