Important
The Community.OData.Linq
is no longer maintained. If you need modern OData features try new OData2Linq package that supposed to replace this one.
Use OData filter text query in linq expresson for any IQuerable. Support web and desktop applications.
Please check samples below to get started:
https://dotnetfiddle.net/7Ndwot
using System;
using System.Linq;
using Community.OData.Linq;
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);
}
}
}
Use .ToOriginalQuery()
after finishing working with OData to be able to support provider specific methods of original query.
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()
var item = await Container.GetItemLinqQueryable<TestEntity>().OData()
.Filter($"Id eq '{id1}'")
.TopSkip("1")
.ToOriginalQuery() // required to be able to use .ToFeedIterator() next.
.ToFeedIterator()
.ReadNextAsync()
https://github.com/IharYakimush/comminity-data-odata-linq/wiki
Params | In Memory Collections | Entity Framework | CosmosDB SQL API |
---|---|---|---|
$filter | + | + | + |
$orderby | + | + | + |
$select | + | + | - |
$expand | + | + | - |
$top | + | + | + |
$skip | + | + | + |
- https://www.nuget.org/packages/Community.OData.Linq
- https://www.nuget.org/packages/Community.OData.Linq.Json
- https://www.nuget.org/packages/Community.OData.Linq.AspNetCore
Please feel free to create issues and pool requests to develop branch
Majority of the code was taken from https://github.com/OData/WebApi