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

Commit 9fa5114

Browse files
author
Ihar Yakimush
committed
nuget package
1 parent cba23fa commit 9fa5114

File tree

7 files changed

+95
-63
lines changed

7 files changed

+95
-63
lines changed

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

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,10 @@
44
<TargetFramework>netcoreapp2.0</TargetFramework>
55

66
<IsPackable>false</IsPackable>
7+
8+
<SignAssembly>true</SignAssembly>
9+
10+
<AssemblyOriginatorKeyFile>Sign.pfx</AssemblyOriginatorKeyFile>
711
</PropertyGroup>
812

913
<ItemGroup>

Community.Data.OData.Linq.xTests/ODataQueryTests.cs

Lines changed: 0 additions & 20 deletions
This file was deleted.

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

Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,19 @@
22

33
<PropertyGroup>
44
<TargetFrameworks>netstandard2.0;net45</TargetFrameworks>
5+
<SignAssembly>true</SignAssembly>
6+
<AssemblyOriginatorKeyFile>Sign.pfx</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 linq filter</PackageTags>
14+
<PackageReleaseNotes />
15+
<Description>Use OData text filter in LINQ for any IQuerable without ASP.NET dependency</Description>
16+
<Company />
17+
<RepositoryUrl></RepositoryUrl>
518
</PropertyGroup>
619

720
<ItemGroup>
Lines changed: 32 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,12 +1,41 @@
11
namespace Community.OData.Linq
22
{
33
using System;
4+
using System.Collections;
5+
using System.Collections.Generic;
46
using System.Linq;
7+
using System.Linq.Expressions;
58

6-
public class ODataQueryOrdered<T> : ODataQuery<T>
9+
using Microsoft.Extensions.DependencyInjection;
10+
using Microsoft.OData.Edm;
11+
12+
public class ODataQuery<T> : IQueryable<T>
713
{
8-
public ODataQueryOrdered(IOrderedQueryable inner, IServiceProvider serviceProvider):base(inner,serviceProvider)
14+
internal ODataQuery(IQueryable inner, IServiceProvider serviceProvider)
15+
{
16+
this.Inner = inner ?? throw new ArgumentNullException(nameof(inner));
17+
this.ServiceProvider = serviceProvider;
18+
}
19+
20+
public IEdmModel EdmModel => this.ServiceProvider.GetRequiredService<IEdmModel>();
21+
22+
public Type ElementType => this.Inner.ElementType;
23+
24+
public Expression Expression => this.Inner.Expression;
25+
26+
public IQueryProvider Provider => this.Inner.Provider;
27+
28+
public IQueryable Inner { get; }
29+
public IServiceProvider ServiceProvider { get; }
30+
31+
public IEnumerator GetEnumerator()
32+
{
33+
return this.Inner.GetEnumerator();
34+
}
35+
36+
IEnumerator<T> IEnumerable<T>.GetEnumerator()
937
{
10-
}
38+
return (IEnumerator<T>)this.Inner.GetEnumerator();
39+
}
1140
}
1241
}
Lines changed: 3 additions & 32 deletions
Original file line numberDiff line numberDiff line change
@@ -1,41 +1,12 @@
11
namespace Community.OData.Linq
22
{
33
using System;
4-
using System.Collections;
5-
using System.Collections.Generic;
64
using System.Linq;
7-
using System.Linq.Expressions;
85

9-
using Microsoft.Extensions.DependencyInjection;
10-
using Microsoft.OData.Edm;
11-
12-
public class ODataQuery<T> : IQueryable<T>, IOrderedQueryable<T>
6+
public class ODataQueryOrdered<T> : ODataQuery<T>, IOrderedQueryable<T>
137
{
14-
public ODataQuery(IQueryable inner, IServiceProvider serviceProvider)
15-
{
16-
this.Inner = inner ?? throw new ArgumentNullException(nameof(inner));
17-
this.ServiceProvider = serviceProvider;
18-
}
19-
20-
public IEdmModel EdmModel => this.ServiceProvider.GetRequiredService<IEdmModel>();
21-
22-
public Type ElementType => this.Inner.ElementType;
23-
24-
public Expression Expression => this.Inner.Expression;
25-
26-
public IQueryProvider Provider => this.Inner.Provider;
27-
28-
public IQueryable Inner { get; }
29-
public IServiceProvider ServiceProvider { get; }
30-
31-
public IEnumerator GetEnumerator()
32-
{
33-
return this.Inner.GetEnumerator();
34-
}
35-
36-
IEnumerator<T> IEnumerable<T>.GetEnumerator()
8+
internal ODataQueryOrdered(IOrderedQueryable inner, IServiceProvider serviceProvider):base(inner,serviceProvider)
379
{
38-
return (IEnumerator<T>)this.Inner.GetEnumerator();
39-
}
10+
}
4011
}
4112
}

Community.Data.OData.Linq/OdataLinqExtensions.cs

Lines changed: 42 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -55,13 +55,23 @@ public static class ODataLinqExtensions
5555
/// </returns>
5656
public static ODataQuery<T> OData<T>(this IQueryable<T> query, Action<ODataSettings> configuration = null, IEdmModel edmModel = null)
5757
{
58+
if (query == null) throw new ArgumentNullException(nameof(query));
59+
5860
if (edmModel == null)
59-
{
61+
{
6062
ODataConventionModelBuilder builder = new ODataConventionModelBuilder();
6163
builder.AddEntityType(typeof(T));
6264
builder.AddEntitySet(typeof(T).Name, new EntityTypeConfiguration(new ODataModelBuilder(), typeof(T)));
6365
edmModel = builder.GetEdmModel();
6466
}
67+
else
68+
{
69+
if (edmModel.SchemaElements.Count(e => e.SchemaElementKind == EdmSchemaElementKind.EntityContainer) == 0)
70+
{
71+
throw new ArgumentException("Provided Entity Model have no IEdmEntityContainer", nameof(edmModel));
72+
73+
}
74+
}
6575

6676
ODataSettings settings = new ODataSettings();
6777
configuration?.Invoke(settings);
@@ -83,6 +93,9 @@ public static ODataQuery<T> OData<T>(this IQueryable<T> query, Action<ODataSetti
8393

8494
public static ODataQuery<T> Filter<T>(this ODataQuery<T> query, string filterText, string entitySetName = null)
8595
{
96+
if (query == null) throw new ArgumentNullException(nameof(query));
97+
if (filterText == null) throw new ArgumentNullException(nameof(filterText));
98+
8699
IEdmModel edmModel = query.EdmModel;
87100

88101
ODataQueryOptionParser queryOptionParser = GetParser(query, entitySetName,
@@ -110,6 +123,9 @@ public static ODataQuery<T> Filter<T>(this ODataQuery<T> query, string filterTex
110123

111124
public static IOrderedQueryable<T> OrderBy<T>(this ODataQuery<T> query, string orderbyText, string entitySetName = null)
112125
{
126+
if (query == null) throw new ArgumentNullException(nameof(query));
127+
if (orderbyText == null) throw new ArgumentNullException(nameof(orderbyText));
128+
113129
IEdmModel edmModel = query.EdmModel;
114130

115131
ODataQueryOptionParser queryOptionParser = GetParser(query, entitySetName,
@@ -132,8 +148,6 @@ public static IOrderedQueryable<T> OrderBy<T>(this ODataQuery<T> query, string o
132148

133149
private static IOrderedQueryable OrderApplyToCore<T>(ODataQuery<T> query, ODataQuerySettings querySettings, ICollection<OrderByNode> nodes, IEdmModel model)
134150
{
135-
136-
137151
bool alreadyOrdered = false;
138152
IQueryable querySoFar = query;
139153

@@ -240,11 +254,32 @@ private static ODataQueryOptionParser GetParser<T>(ODataQuery<T> query,string en
240254
entitySetName = typeof(T).Name;
241255
}
242256

243-
IEdmEntityContainer container =
244-
(IEdmEntityContainer)edmModel.SchemaElements.Single(
245-
e => e.SchemaElementKind == EdmSchemaElementKind.EntityContainer);
257+
IEdmEntityContainer[] containers =
258+
edmModel.SchemaElements.Where(
259+
e => e.SchemaElementKind == EdmSchemaElementKind.EntityContainer &&
260+
(e as IEdmEntityContainer).FindEntitySet(entitySetName) != null)
261+
.OfType<IEdmEntityContainer>()
262+
.ToArray();
263+
264+
if (containers.Length == 0)
265+
{
266+
throw new ArgumentException($"Unable to find {entitySetName} entity set in the model.",
267+
nameof(entitySetName));
268+
}
269+
270+
if (containers.Length > 1)
271+
{
272+
throw new ArgumentException($"Entity Set {entitySetName} found more that 1 time",
273+
nameof(entitySetName));
274+
}
275+
276+
IEdmEntitySet entitySet = containers.Single().FindEntitySet(entitySetName);
277+
278+
if (entitySet == null)
279+
{
280+
281+
}
246282

247-
IEdmEntitySet entitySet = container.FindEntitySet(entitySetName);
248283
ODataPath path = new ODataPath(new EntitySetSegment(entitySet));
249284

250285
return new ODataQueryOptionParser(

LICENSE

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
MIT License
22

33
Copyright (c) 2018 Microsoft. All rights reserved.
4-
Modification Copyright (c) 2018 IharYakimush
4+
Modification Copyright (c) 2018 Ihar Yakimush
55

66
Permission is hereby granted, free of charge, to any person obtaining a copy
77
of this software and associated documentation files (the "Software"), to deal

0 commit comments

Comments
 (0)