Skip to content
This repository has been archived by the owner on Dec 29, 2020. It is now read-only.

Commit

Permalink
Simplified select logic
Browse files Browse the repository at this point in the history
  • Loading branch information
TrevorPilley committed May 13, 2020
1 parent 7237d51 commit c8a7455
Show file tree
Hide file tree
Showing 2 changed files with 12 additions and 17 deletions.
6 changes: 3 additions & 3 deletions Net.Http.OData.Tests/Linq/ODataQueryOptionsExtensionsTests.cs
Original file line number Diff line number Diff line change
Expand Up @@ -178,12 +178,12 @@ public void ApplyTo_Throws_ArgumentNullException_For_Null_Queryable()
EntityDataModel.Current.EntitySets["Customers"],
Mock.Of<IODataQueryOptionsValidator>());

Assert.Throws<ArgumentNullException>(() => ODataQueryOptionsExtensions.ApplyTo(queryOptions, null).ToList());
Assert.Throws<ArgumentNullException>(() => ODataQueryOptionsExtensions.ApplyTo(queryOptions, null));
}

[Fact]
public void ApplyTo_Throws_ArgumentNullException_For_Null_QueryOptions()
=> Assert.Throws<ArgumentNullException>(() => ODataQueryOptionsExtensions.ApplyTo(null, _categories.AsQueryable()).ToList());
=> Assert.Throws<ArgumentNullException>(() => ODataQueryOptionsExtensions.ApplyTo(null, _categories.AsQueryable()));

[Fact]
public void ApplyTo_Throws_InvalidOperationException_For_Incorrect_QueryType()
Expand All @@ -195,7 +195,7 @@ public void ApplyTo_Throws_InvalidOperationException_For_Incorrect_QueryType()
EntityDataModel.Current.EntitySets["Customers"],
Mock.Of<IODataQueryOptionsValidator>());

Assert.Throws<InvalidOperationException>(() => queryOptions.ApplyTo(_categories.AsQueryable()).ToList());
Assert.Throws<InvalidOperationException>(() => queryOptions.ApplyTo(_categories.AsQueryable()));
}
}
}
23 changes: 9 additions & 14 deletions Net.Http.OData/Linq/ODataQueryOptionsExtensions.cs
Original file line number Diff line number Diff line change
Expand Up @@ -47,6 +47,11 @@ public static IEnumerable<ExpandoObject> ApplyTo(this ODataQueryOptions queryOpt
throw new InvalidOperationException();
}

return ApplyToImpl(queryOptions, queryable);
}

private static IEnumerable<ExpandoObject> ApplyToImpl(ODataQueryOptions queryOptions, IQueryable queryable)
{
foreach (object entity in queryable)
{
yield return BuildExpando(queryOptions, entity);
Expand All @@ -55,22 +60,10 @@ public static IEnumerable<ExpandoObject> ApplyTo(this ODataQueryOptions queryOpt

private static ExpandoObject BuildExpando(ODataQueryOptions queryOptions, object entity)
{
var expandoObject = new ExpandoObject();
IEnumerable<PropertyPath> propertyPaths = queryOptions.Select?.PropertyPaths ?? queryOptions.EntitySet.EdmType.Properties.Where(p => !p.IsNavigable).Select(PropertyPath.For);

if (queryOptions.Select == null)
{
PopulateExpando(expandoObject, entity, queryOptions.EntitySet.EdmType.Properties.Where(p => !p.IsNavigable).Select(PropertyPath.For));
}
else
{
PopulateExpando(expandoObject, entity, queryOptions.Select.PropertyPaths);
}

return expandoObject;
}
var expandoObject = new ExpandoObject();

private static void PopulateExpando(ExpandoObject expandoObject, object entity, IEnumerable<PropertyPath> propertyPaths)
{
foreach (PropertyPath propertyPath in propertyPaths)
{
PropertyPath path = propertyPath;
Expand All @@ -87,6 +80,8 @@ private static void PopulateExpando(ExpandoObject expandoObject, object entity,

dictionary[path.Property.Name] = path.Property.ClrProperty.GetValue(obj);
}

return expandoObject;
}
}
}

0 comments on commit c8a7455

Please sign in to comment.