Skip to content
This repository was archived by the owner on Feb 17, 2025. It is now read-only.

Commit 0834501

Browse files
Ihar YakimushIhar Yakimush
authored andcommitted
support select expand in json format
1 parent 2a8d54a commit 0834501

File tree

12 files changed

+391
-7
lines changed

12 files changed

+391
-7
lines changed

Community.Data.OData.Linq.sln

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -16,6 +16,8 @@ Project("{2150E333-8FDC-42A3-9474-1A3956D46DE8}") = "Solution Items", "Solution
1616
EndProject
1717
Project("{FAE04EC0-301F-11D3-BF4B-00C04F79EFBC}") = "Community.OData.Linq.Demo", "Community.OData.Linq.Demo\Community.OData.Linq.Demo.csproj", "{DE428072-2C49-4F4A-8F2E-DB976C0BC797}"
1818
EndProject
19+
Project("{FAE04EC0-301F-11D3-BF4B-00C04F79EFBC}") = "Community.OData.Linq.Json", "Community.OData.Linq.Json\Community.OData.Linq.Json.csproj", "{6C454F01-28D1-49B1-BE98-244E5DA2D095}"
20+
EndProject
1921
Global
2022
GlobalSection(SolutionConfigurationPlatforms) = preSolution
2123
CodeAnalysis|Any CPU = CodeAnalysis|Any CPU
@@ -41,6 +43,12 @@ Global
4143
{DE428072-2C49-4F4A-8F2E-DB976C0BC797}.Debug|Any CPU.Build.0 = Debug|Any CPU
4244
{DE428072-2C49-4F4A-8F2E-DB976C0BC797}.Release|Any CPU.ActiveCfg = Release|Any CPU
4345
{DE428072-2C49-4F4A-8F2E-DB976C0BC797}.Release|Any CPU.Build.0 = Release|Any CPU
46+
{6C454F01-28D1-49B1-BE98-244E5DA2D095}.CodeAnalysis|Any CPU.ActiveCfg = Debug|Any CPU
47+
{6C454F01-28D1-49B1-BE98-244E5DA2D095}.CodeAnalysis|Any CPU.Build.0 = Debug|Any CPU
48+
{6C454F01-28D1-49B1-BE98-244E5DA2D095}.Debug|Any CPU.ActiveCfg = Debug|Any CPU
49+
{6C454F01-28D1-49B1-BE98-244E5DA2D095}.Debug|Any CPU.Build.0 = Debug|Any CPU
50+
{6C454F01-28D1-49B1-BE98-244E5DA2D095}.Release|Any CPU.ActiveCfg = Release|Any CPU
51+
{6C454F01-28D1-49B1-BE98-244E5DA2D095}.Release|Any CPU.Build.0 = Release|Any CPU
4452
EndGlobalSection
4553
GlobalSection(SolutionProperties) = preSolution
4654
HideSolutionNode = FALSE

Community.Data.OData.Linq.xTests/Community.OData.Linq.xTests.csproj

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -25,6 +25,7 @@
2525

2626
<ItemGroup>
2727
<ProjectReference Include="..\Community.Data.OData.Linq\Community.OData.Linq.csproj" />
28+
<ProjectReference Include="..\Community.OData.Linq.Json\Community.OData.Linq.Json.csproj" />
2829
</ItemGroup>
2930

3031
</Project>
Lines changed: 28 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,28 @@
1+
namespace Community.OData.Linq.xTests
2+
{
3+
using System.Linq;
4+
5+
using Community.OData.Linq.xTests.SampleData;
6+
using Community.OData.Linq.Json;
7+
8+
using Newtonsoft.Json;
9+
using Newtonsoft.Json.Linq;
10+
11+
using Xunit;
12+
13+
public class JsonTests
14+
{
15+
[Fact]
16+
public void SerializeSelectExpand()
17+
{
18+
string result = ClassWithCollection.CreateQuery().OData().SelectExpandJsonString("Name", "Link2($filter=Id eq 311;$select=Name)");
19+
20+
Assert.NotNull(result);
21+
22+
JToken token = ClassWithCollection.CreateQuery().OData().SelectExpandJsonToken("Name", "Link2($filter=Id eq 311;$select=Name)");
23+
Assert.NotNull(token);
24+
25+
Assert.Equal(result, token.ToString(Formatting.None));
26+
}
27+
}
28+
}

Community.Data.OData.Linq/Community.OData.Linq.csproj

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -11,14 +11,14 @@
1111
<PackageProjectUrl>https://github.com/IharYakimush/comminity-data-odata-linq</PackageProjectUrl>
1212
<RepositoryType></RepositoryType>
1313
<PackageTags>odata filter linq</PackageTags>
14-
<PackageReleaseNotes />
14+
<PackageReleaseNotes>Added support for $select and $expand parameters</PackageReleaseNotes>
1515
<Description>Use OData filter text query in linq expresson for any IQuerable without ASP.NET dependency</Description>
1616
<Company />
1717
<RepositoryUrl></RepositoryUrl>
18-
<Version>1.0.2</Version>
18+
<Version>1.0.3</Version>
1919
<NeutralLanguage>en</NeutralLanguage>
20-
<AssemblyVersion>1.0.2.0</AssemblyVersion>
21-
<FileVersion>1.0.2.0</FileVersion>
20+
<AssemblyVersion>1.0.3.0</AssemblyVersion>
21+
<FileVersion>1.0.3.0</FileVersion>
2222
</PropertyGroup>
2323

2424
<ItemGroup>

Community.Data.OData.Linq/OdataLinqExtensions.cs

Lines changed: 68 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -82,6 +82,27 @@ public static ODataQuery<T> OData<T>(this IQueryable<T> query, Action<ODataSetti
8282
return dataQuery;
8383
}
8484

85+
/// <summary>
86+
/// The select and expand query options.
87+
/// </summary>
88+
/// <param name="query">
89+
/// The OData aware query.
90+
/// </param>
91+
/// <param name="selectText">
92+
/// The $select parameter text.
93+
/// </param>
94+
/// <param name="expandText">
95+
/// The $expand parameter text.
96+
/// </param>
97+
/// <param name="entitySetName">
98+
/// The entity set name.
99+
/// </param>
100+
/// <typeparam name="T">
101+
/// The query type param
102+
/// </typeparam>
103+
/// <returns>
104+
/// The <see cref="IEnumerable{ISelectExpandWrapper}"/> selection result in specific format.
105+
/// </returns>
85106
public static IEnumerable<ISelectExpandWrapper> SelectExpand<T>(
86107
this ODataQuery<T> query,
87108
string selectText = null,
@@ -100,6 +121,27 @@ public static IEnumerable<ISelectExpandWrapper> SelectExpand<T>(
100121
return result.OfType<ISelectExpandWrapper>();
101122
}
102123

124+
/// <summary>
125+
/// The Filter.
126+
/// </summary>
127+
/// <param name="query">
128+
/// The OData aware query.
129+
/// </param>
130+
/// <param name="filterText">
131+
/// The $filter parameter text.
132+
/// </param>
133+
/// <param name="entitySetName">
134+
/// The entity set name.
135+
/// </param>
136+
/// <typeparam name="T">
137+
/// The query type param
138+
/// </typeparam>
139+
/// <returns>
140+
/// The <see cref="ODataQuery{T}"/> query with applied filter parameter.
141+
/// </returns>
142+
/// <exception cref="ArgumentNullException">
143+
/// Argument Null Exception
144+
/// </exception>
103145
public static ODataQuery<T> Filter<T>(this ODataQuery<T> query, string filterText, string entitySetName = null)
104146
{
105147
if (query == null) throw new ArgumentNullException(nameof(query));
@@ -130,15 +172,38 @@ public static ODataQuery<T> Filter<T>(this ODataQuery<T> query, string filterTex
130172
return new ODataQuery<T>(result, query.ServiceProvider);
131173
}
132174

175+
/// <summary>
176+
/// The OrderBy.
177+
/// </summary>
178+
/// <param name="query">
179+
/// The OData aware query.
180+
/// </param>
181+
/// <param name="orderbyText">
182+
/// The $orderby parameter text.
183+
/// </param>
184+
/// <param name="entitySetName">
185+
/// The entity set name.
186+
/// </param>
187+
/// <typeparam name="T">
188+
/// The query type param
189+
/// </typeparam>
190+
/// <returns>
191+
/// The <see cref="ODataQuery{T}"/> query with applied order by parameter.
192+
/// </returns>
193+
/// <exception cref="ArgumentNullException">
194+
/// Argument Null Exception
195+
/// </exception>
133196
public static IOrderedQueryable<T> OrderBy<T>(this ODataQuery<T> query, string orderbyText, string entitySetName = null)
134197
{
135198
if (query == null) throw new ArgumentNullException(nameof(query));
136199
if (orderbyText == null) throw new ArgumentNullException(nameof(orderbyText));
137200

138201
IEdmModel edmModel = query.EdmModel;
139-
140-
ODataQueryOptionParser queryOptionParser = GetParser(query, entitySetName,
141-
new Dictionary<string, string> { { "$orderby", orderbyText } });
202+
203+
ODataQueryOptionParser queryOptionParser = GetParser(
204+
query,
205+
entitySetName,
206+
new Dictionary<string, string> { { "$orderby", orderbyText } });
142207

143208
ODataSettings settings = query.ServiceProvider.GetRequiredService<ODataSettings>();
144209

Community.OData.Linq.Demo/Community.OData.Linq.Demo.csproj

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -5,8 +5,13 @@
55
<TargetFramework>netcoreapp2.0</TargetFramework>
66
</PropertyGroup>
77

8+
<ItemGroup>
9+
<PackageReference Include="Newtonsoft.Json" Version="10.0.3" />
10+
</ItemGroup>
11+
812
<ItemGroup>
913
<ProjectReference Include="..\Community.Data.OData.Linq\Community.OData.Linq.csproj" />
14+
<ProjectReference Include="..\Community.OData.Linq.Json\Community.OData.Linq.Json.csproj" />
1015
</ItemGroup>
1116

1217
</Project>

Community.OData.Linq.Demo/Program.cs

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -16,6 +16,8 @@ static void Main(string[] args)
1616

1717
ExpandDemo.SelectExpand1();
1818
ExpandDemo.SelectExpand2();
19+
20+
SelectExpandJsonDemo.SelectExpandToJson();
1921
}
2022
}
2123
}
Lines changed: 64 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,64 @@
1+
namespace Demo
2+
{
3+
using System;
4+
using System.Linq;
5+
6+
using Community.OData.Linq;
7+
using Community.OData.Linq.Json;
8+
9+
using Newtonsoft.Json;
10+
11+
public static class SelectExpandJsonDemo
12+
{
13+
public static void SelectExpandToJson()
14+
{
15+
Console.WriteLine(nameof(SelectExpandToJson));
16+
Console.WriteLine("/*");
17+
18+
IQueryable<Sample> dataSet = Sample.CreateQuerable();
19+
var result = dataSet.OData().SelectExpandJsonToken(
20+
"Name",
21+
"RelatedEntity($select=Id),RelatedEntitiesCollection($filter=Id ge 200;$top=1)");
22+
23+
/*
24+
[
25+
{
26+
"Name": "name1",
27+
"RelatedEntitiesCollection": [],
28+
"RelatedEntity": {
29+
"Id": 10
30+
}
31+
},
32+
{
33+
"Name": "name2",
34+
"RelatedEntitiesCollection": [
35+
{
36+
"Id": 200,
37+
"Name": "name200"
38+
}
39+
],
40+
"RelatedEntity": {
41+
"Id": 20
42+
}
43+
},
44+
{
45+
"Name": "name3",
46+
"RelatedEntitiesCollection": [
47+
{
48+
"Id": 300,
49+
"Name": "name300"
50+
}
51+
],
52+
"RelatedEntity": {
53+
"Id": 30
54+
}
55+
}
56+
]
57+
*/
58+
Console.WriteLine(result.ToString(Formatting.Indented));
59+
60+
Console.WriteLine("*/");
61+
Console.WriteLine(Environment.NewLine);
62+
}
63+
}
64+
}
Lines changed: 32 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,32 @@
1+
<Project Sdk="Microsoft.NET.Sdk">
2+
3+
<PropertyGroup>
4+
<TargetFrameworks>netstandard2.0;net45</TargetFrameworks>
5+
<SignAssembly>true</SignAssembly>
6+
<AssemblyOriginatorKeyFile>sgn.snk</AssemblyOriginatorKeyFile>
7+
<GeneratePackageOnBuild>true</GeneratePackageOnBuild>
8+
<PackageRequireLicenseAcceptance>true</PackageRequireLicenseAcceptance>
9+
<Authors>IharYakimush</Authors>
10+
<PackageLicenseUrl>https://github.com/IharYakimush/comminity-data-odata-linq/blob/master/LICENSE</PackageLicenseUrl>
11+
<PackageProjectUrl>https://github.com/IharYakimush/comminity-data-odata-linq</PackageProjectUrl>
12+
<RepositoryType></RepositoryType>
13+
<PackageTags>odata filter linq json</PackageTags>
14+
<PackageReleaseNotes>Json serialization for SelectExpand result</PackageReleaseNotes>
15+
<Description>Json serialization for SelectExpand result</Description>
16+
<Company />
17+
<RepositoryUrl></RepositoryUrl>
18+
<Version>1.0.3</Version>
19+
<NeutralLanguage>en</NeutralLanguage>
20+
<AssemblyVersion>1.0.3.0</AssemblyVersion>
21+
<FileVersion>1.0.3.0</FileVersion>
22+
</PropertyGroup>
23+
24+
<ItemGroup>
25+
<PackageReference Include="Newtonsoft.Json" Version="10.0.3" />
26+
</ItemGroup>
27+
28+
<ItemGroup>
29+
<ProjectReference Include="..\Community.Data.OData.Linq\Community.OData.Linq.csproj" />
30+
</ItemGroup>
31+
32+
</Project>
Lines changed: 67 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,67 @@
1+
namespace Community.OData.Linq.Json
2+
{
3+
using System;
4+
using System.Collections.Generic;
5+
using System.IO;
6+
using System.Linq;
7+
using System.Text;
8+
9+
using Newtonsoft.Json;
10+
using Newtonsoft.Json.Linq;
11+
12+
public static class ODataLinqExtensions
13+
{
14+
public static string SelectExpandJsonString<T>(
15+
this ODataQuery<T> query,
16+
string selectText = null,
17+
string expandText = null,
18+
Action<JsonSerializer> configureSerializer = null,
19+
string entitySetName = null)
20+
{
21+
StringBuilder stringBuilder = new StringBuilder();
22+
TextWriter textWriter = new StringWriter(stringBuilder);
23+
using (textWriter)
24+
{
25+
JsonWriter writer = new JsonTextWriter(textWriter);
26+
27+
SelectExpandJson(query, writer, selectText, expandText, configureSerializer, entitySetName);
28+
29+
return stringBuilder.ToString();
30+
}
31+
}
32+
33+
public static JToken SelectExpandJsonToken<T>(
34+
this ODataQuery<T> query,
35+
string selectText = null,
36+
string expandText = null,
37+
Action<JsonSerializer> configureSerializer = null,
38+
string entitySetName = null)
39+
{
40+
JTokenWriter writer = new JTokenWriter();
41+
using (writer)
42+
{
43+
SelectExpandJson(query, writer, selectText, expandText, configureSerializer, entitySetName);
44+
45+
return writer.Token;
46+
}
47+
}
48+
49+
public static void SelectExpandJson<T>(
50+
this ODataQuery<T> query,
51+
JsonWriter writer,
52+
string selectText = null,
53+
string expandText = null,
54+
Action<JsonSerializer> configureSerializer = null,
55+
string entitySetName = null)
56+
{
57+
ISelectExpandWrapper[] result = query.SelectExpand(selectText, expandText, entitySetName).ToArray();
58+
59+
JsonSerializer serializer = new JsonSerializer();
60+
61+
configureSerializer?.Invoke(serializer);
62+
serializer.Converters.Add(new SelectExpandWrapperConverter());
63+
64+
serializer.Serialize(writer, result);
65+
}
66+
}
67+
}

0 commit comments

Comments
 (0)