Skip to content

Commit e763f2f

Browse files
committed
#97 Extensions: Where()
1 parent 5e2ddf7 commit e763f2f

2 files changed

Lines changed: 43 additions & 1 deletion

File tree

Neomaster.JsonToLinq/Application/JsonLinq.cs

Lines changed: 42 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -8,6 +8,48 @@ namespace Neomaster.JsonToLinq;
88
/// </summary>
99
public static class JsonLinq
1010
{
11+
/// <summary>
12+
/// Filters a sequence of values based on a JSON filter definition.
13+
/// </summary>
14+
/// <typeparam name="TElement">Type of elements to filter.</typeparam>
15+
/// <param name="elements">An System.Collections.Generic.IEnumerable`1 to filter.</param>
16+
/// <param name="json">JSON filter definition.</param>
17+
/// <param name="fieldMapper">Maps JSON field names to <see cref="ExpressionField"/> definitions.</param>
18+
/// <param name="options">Optional parser settings. Uses defaults if null.</param>
19+
/// <returns>
20+
/// An System.Collections.Generic.IEnumerable`1
21+
/// that contains elements from the input sequence that satisfy the condition.
22+
/// </returns>
23+
public static IEnumerable<TElement> Where<TElement>(
24+
this IEnumerable<TElement> elements,
25+
string json,
26+
ExpressionFieldMapper fieldMapper = null,
27+
ExpressionParsingOptions options = null)
28+
{
29+
return elements.Where(JsonDocument.Parse(json), fieldMapper, options);
30+
}
31+
32+
/// <summary>
33+
/// Filters a sequence of values based on a JSON filter definition.
34+
/// </summary>
35+
/// <typeparam name="TElement">Type of elements to filter.</typeparam>
36+
/// <param name="elements">An System.Collections.Generic.IEnumerable`1 to filter.</param>
37+
/// <param name="doc">JSON filter definition.</param>
38+
/// <param name="fieldMapper">Maps JSON field names to <see cref="ExpressionField"/> definitions.</param>
39+
/// <param name="options">Optional parser settings. Uses defaults if null.</param>
40+
/// <returns>
41+
/// An System.Collections.Generic.IEnumerable`1
42+
/// that contains elements from the input sequence that satisfy the condition.
43+
/// </returns>
44+
public static IEnumerable<TElement> Where<TElement>(
45+
this IEnumerable<TElement> elements,
46+
JsonDocument doc,
47+
ExpressionFieldMapper fieldMapper = null,
48+
ExpressionParsingOptions options = null)
49+
{
50+
return elements.Where(ParseToFilterExpression<TElement>(doc, fieldMapper, options).Compile());
51+
}
52+
1153
/// <summary>
1254
/// Parses a JSON document into a LINQ expression filter for <typeparamref name="T"/>.
1355
/// </summary>

Neomaster.JsonToLinq/Neomaster.JsonToLinq.csproj

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -9,7 +9,7 @@
99
<NoWarn>$(NoWarn);IDE0079</NoWarn>
1010
<PackageId>JsonToLinq</PackageId>
1111
<PackageIcon>logo.png</PackageIcon>
12-
<Version>1.0.0-rc903</Version>
12+
<Version>1.0.0-rc904</Version>
1313
<Authors>Neomaster</Authors>
1414
<Company>Neomaster</Company>
1515
<Copyright>Copyright © 2025 Neomaster</Copyright>

0 commit comments

Comments
 (0)