forked from OData/AspNetCoreOData
-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Add OData to Linq support with the OData2Linq project (#1)
* Add OData2Linq project * Add test and benchmark projects * Add extension methods and unit tests from the Community.OData.Linq nuget
- Loading branch information
Showing
51 changed files
with
3,092 additions
and
2 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,107 @@ | ||
[](https://www.nuget.org/packages/OData2Linq/) | ||
|
||
# OData2Linq | ||
Apply an OData text query (filter, order by, select, ..) to an IQueryable expression. | ||
|
||
# Features | ||
- Manipulate an IQueryable expression with OData text query | ||
|
||
## Supported OData parameters | ||
| Params | In Memory Collections | Entity Framework | CosmosDB SQL API | | ||
| ------------- |:---------------------:|:----------------:| :---------------:| | ||
| $filter |+ | + | + | | ||
| $orderby |+ | + | + | | ||
| $select |+ | + | - | | ||
| $expand |+ | + | - | | ||
| $top |+ | + | + | | ||
| $skip |+ | + | + | | ||
|
||
# Samples | ||
Please check samples below to get started | ||
|
||
## .NET Fiddle | ||
https://dotnetfiddle.net/6dLB2g | ||
|
||
## Console app | ||
```csharp | ||
using System; | ||
using System.Linq; | ||
using OData2Linq; | ||
|
||
public class Entity | ||
{ | ||
public int Id { get; set; } | ||
public string Name { get; set; } | ||
} | ||
|
||
public static class GetStartedDemo | ||
{ | ||
public static void Demo() | ||
{ | ||
Entity[] items = | ||
{ | ||
new Entity { Id = 1, Name = "n1" }, | ||
new Entity { Id = 2, Name = "n2" }, | ||
new Entity { Id = 3, Name = "n3" } | ||
}; | ||
IQueryable<Entity> query = items.AsQueryable(); | ||
|
||
var result = query.OData().Filter("Id eq 1 or Name eq 'n3'").OrderBy("Name desc").TopSkip("10", "0").ToArray(); | ||
|
||
// Id: 3 Name: n3 | ||
// Id: 1 Name: n1 | ||
foreach (Entity entity in result) | ||
{ | ||
Console.WriteLine("Id: {0} Name: {1}", entity.Id, entity.Name); | ||
} | ||
} | ||
} | ||
``` | ||
|
||
## Support ToArrayAsync(), ToListAsync(), and all other provider specific methods. | ||
Use `.ToOriginalQuery()` after finishing working with OData to be able to support provider specific methods of original query. | ||
|
||
### Entity Framework async data fetch. | ||
``` | ||
Student[] array = await dbContext.Students.OData() | ||
.Filter("LastName eq 'Alexander' or FirstMidName eq 'Laura'") | ||
.OrderBy("EnrollmentDate desc") | ||
.TopSkip("1","1") | ||
.ToOriginalQuery() // required to be able to use .ToArrayAsync() next. | ||
.ToArrayAsync(); | ||
ISelectExpandWrapper[] select2 = await dbContext.Students.OData() | ||
.Filter("LastName eq 'Alexander' or FirstMidName eq 'Laura'") | ||
.OrderBy("EnrollmentDate desc") | ||
.SelectExpandAsQueryable("LastName", "Enrollments($select=CourseId)") //.SelectExpandAsQueryable() use .ToOriginalQuery() implicitly, so not need to call it. | ||
.ToArrayAsync() | ||
``` | ||
### CosmosDb SQL API async data fetch. | ||
``` | ||
var item = await Container.GetItemLinqQueryable<TestEntity>().OData() | ||
.Filter($"Id eq '{id1}'") | ||
.TopSkip("1") | ||
.ToOriginalQuery() // required to be able to use .ToFeedIterator() next. | ||
.ToFeedIterator() | ||
.ReadNextAsync() | ||
``` | ||
|
||
## Advanced code samples at wiki | ||
See the [Wiki pages](https://github.com/ArnaudB88/OData2Linq/wiki) | ||
|
||
# Contribution | ||
Please feel free to create issues and pull requests to the main branch. | ||
|
||
# Nuget | ||
| Package | NuGet | Info | | ||
|---------------------------------|----------------------------------------------------------------------------------------------------------------------------------------------------|------------------| | ||
| OData2Linq | [](https://www.nuget.org/packages/OData2Linq/) | OData v8 support | | ||
| Community.OData.Linq.Json | [](https://www.nuget.org/packages/Community.OData.Linq.Json) | Still works on odata v7 libraries. Open an issue to create a new package for odata v8. | | ||
| Community.OData.Linq.AspNetCore | [](https://www.nuget.org/packages/Community.OData.Linq.AspNetCore) | Still works on odata v7 libraries. Open an issue to create a new package for odata v8. | | ||
|
||
# References | ||
This project is based on the following project: | ||
https://github.com/IharYakimush/comminity-data-odata-linq | ||
|
||
The repository is a fork from: | ||
https://github.com/OData/AspNetCoreOData |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,34 @@ | ||
# This workflow will build a .NET project | ||
# For more information see: https://docs.github.com/en/actions/automating-builds-and-tests/building-and-testing-net | ||
|
||
name: odata2linq-cd | ||
|
||
on: | ||
push: | ||
tags: | ||
- 'OData2Linq_*.*.*' | ||
|
||
jobs: | ||
build: | ||
|
||
runs-on: windows-latest | ||
strategy: | ||
matrix: | ||
dotnet-version: ['6.0.x'] | ||
|
||
steps: | ||
- uses: actions/checkout@v4 | ||
- name: Setup dotnet ${{ matrix.dotnet-version }} | ||
uses: actions/setup-dotnet@v4 | ||
with: | ||
dotnet-version: ${{ matrix.dotnet-version }} | ||
- name: Restore dependencies | ||
run: dotnet restore ./AspNetCoreOData.sln | ||
- name: Build | ||
run: dotnet build ./AspNetCoreOData.sln -c Release --no-restore | ||
- name: Test | ||
run: dotnet test ./AspNetCoreOData.sln -c Release --no-build --verbosity normal | ||
- name: Pack | ||
run: dotnet pack ./src/OData2Linq/OData2Linq.csproj -c Release -o .\artifacts --no-build | ||
- name: Publish to NuGet | ||
run: dotnet nuget push .\artifacts\*.nupkg --source https://api.nuget.org/v3/index.json --api-key ${{ secrets.NUGET_API_KEY }} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,31 @@ | ||
# This workflow will build a .NET project | ||
# For more information see: https://docs.github.com/en/actions/automating-builds-and-tests/building-and-testing-net | ||
|
||
name: odata2linq-ci | ||
|
||
on: | ||
push: | ||
branches: [ "main" ] | ||
pull_request: | ||
branches: [ "main" ] | ||
|
||
jobs: | ||
build: | ||
|
||
runs-on: windows-latest | ||
strategy: | ||
matrix: | ||
dotnet-version: ['6.0.x'] | ||
|
||
steps: | ||
- uses: actions/checkout@v4 | ||
- name: Setup dotnet ${{ matrix.dotnet-version }} | ||
uses: actions/setup-dotnet@v4 | ||
with: | ||
dotnet-version: ${{ matrix.dotnet-version }} | ||
- name: Restore dependencies | ||
run: dotnet restore ./AspNetCoreOData.sln | ||
- name: Build | ||
run: dotnet build ./AspNetCoreOData.sln --no-restore | ||
- name: Test | ||
run: dotnet test ./AspNetCoreOData.sln --no-build --verbosity normal |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Binary file not shown.
Oops, something went wrong.