Simple fluent dotnet OData query builder.
The goal of this project is to provide an easy way of building OData query parameters, without the need to manually concatenate strings.
It is allows building queries for systems that don't necessarily conform to the OData standard. For example, Dynamics 365 OData endpoints.
To install OData.Fluent.QueryBuilder
from Visual Studio
, find OData.Fluent.QueryBuilder
in the NuGet
package manager user interface or enter the following command in the package manager console:
Install-Package OData.Fluent.QueryBuilder
To add a link to the main dotnet project, run the following command line:
dotnet add package OData.Fluent.QueryBuilder
var filter = new ODataFilterBuilder()
.Where("Property1 eq 'Test'")
.Build();
Output: Property1 eq 'Test'
var filter = new ODataFilterBuilder()
.Where(filter => filter
.Where("Property2 eq 'Test'")
.Or("Property2 eq 'Test'")
.And("Property3 eq 'Test'"))
.And(filter => filter
.Where("Property4 eq 'Test'"))
.Build();
Output: (Property2 eq 'Test' or Property2 eq 'Test' and Property3 eq 'Test') and (Property4 eq 'Test')
Manually transform filter before outputting the results.
This is useful when you want to apply a filter conditionally.
var filter = new ODataFilterBuilder()
.Where(filter => filter
.Where("Property2 eq 'Test'")
.Or("Property2 eq 'Test'")
.And("Property3 eq 'Test'")
.WithTransform(_ => string.Empty))
.And(filter => filter
.Where("Property4 eq 'Test'"))
.Build();
Output: (Property4 eq 'Test')
var results = new ODataSelectBuilder()
.Property("Test1")
.Property("Test2")
.Build();
var results = new ODataSelectBuilder()
.Properties("Test1", "Test2")
.Build();
Output: Test1,Test2
Build a full request with support for paging.
var oDataRequest = new ODataRequestBuilder<ExampleModel>()
.WithFilter(config => config
.Where("ProjectsPublished/ProjectStage eq Microsoft.Dynamics.DataEntities.ProjStatus'InProcess'")
.Or(filter => filter
.Where("ProjectsPublished/ProjectStage eq Microsoft.Dynamics.DataEntities.ProjStatus'Completed'")
.And($"ProjectsPublished/ActualEndDate le {DateTime.Today.AddMonths(-1).ToString("yyyy-MM-dd")}"))
.WithParameterName("filters")
.WithTransform(filter => $"({filter})"))
.WithPaging(config => config
.WithPageSize(10)
.WithParameterNames("limit", "offset")
.NextPageWhen((results, _) => results.Count > 0));
var results = await oDataRequest.ExecuteAsync(async request =>
{
var response = await client.GetAsync($"api/v1/data?{request.Query}");
var data = await response.Content.ReadFromJsonAsync<IEnumerable<ExampleModel>>();
return data ?? Enumerable.Empty<ExampleModel>();
});
Results will be combined and returned as a single collection.