Ho un lavoro (semplificato) ODataController
con il seguente metodo.Come istanziare ODataQueryOptions
public class MyTypeController : ODataController
{
[HttpGet]
[EnableQuery]
[ODataRoute("myTypes")]
public IQueryable<MyType> GetMyTypes(ODataQueryOptions<MyType> options)
{
return _repo.myResultsAsQueryable();
}
}
Vorrei essere in grado di chiamare questo metodo dal server e per fare questo ho bisogno di creare un'istanza di un ODataQueryOptions
che richiede un ODataQueryContext
.
Esistono esempi di come eseguire questa operazione (ad esempio here e here) ma sembrano tutti fare riferimento a una versione precedente di OData. Il costruttore ODataQueryContext richiede attualmente un terzo argomento (percorso ODataPath
) che non è indirizzato in nessun esempio che riesca a trovare.
Edit: @snow_FFFFFF, ecco un po 'di contesto ... mi rendo conto che posso semplicemente consumare l'endpoint OData tramite un HttpClient ma vorrei interagire con l'IQueryable direttamente come dici tu.
Il problema è che l'applicazione su cui sto lavorando consente agli utenti di creare filtri (come un motore di ricerca sofisticato) che possono essere salvati e successivamente richiamati da altri utenti. Da un client JS, cercano semplicemente il filtro per id e inviano una query sull'endpoint OData con il filtro applicato alla stringa di query. Funziona molto bene dal lato client, ma mi piacerebbe essere in grado di fare qualcosa di simile anche dal lato server.
Questo è ciò che mi piacerebbe fare, ma come posso creare un'istanza dell'argomento ODataPath?
public IQueryable<MyType> FilterMyTypes(int filterID)
{
// lookup filter by filterID from db...
filter = "$filter=Status eq 1"; // for example...
ODataPath path = // but how can I get the path!!!
new ODataQueryContext(edmModel, typeof(MyType), path);
var uri = new HttpRequestMessage(HttpMethod.Get, "http://localhost:56339/mytypes?" + filter);
var opts = new ODataQueryOptions<MyType>(ctx, uri);
var results = new MyTypeController().GetMyTypes(opts);
}
Un'altra applicazione di questo sarebbe per sostenere il raggruppamento dinamico come di seguito:
[HttpGet]
[Route("myTypes/{filterID:int}/groupby/{groupByFieldName}")]
public IHttpActionResult GroupMyTypes(int filterID, string groupByFieldName)
{
// For example: get all Active MyTypes and group by AssignedToUserID...
// Get the results of the filter as IQueryable...
var results = FilterMyTypes(filterID);
// group on groupByFieldName
var grouped = results.GroupBy(x => GetPropertyValue(x,groupByFieldName));
// select the groupByFieldName and the count
var transformedResults = grouped.Select(g => new { g.Key, Count = g.Count() });
return Ok(transformedResults);
}
In http://github.com/OData/WebApi, ci sono molti casi di test a cui è possibile fare riferimento. Ad esempio, ODataQueryContext, è possibile fare riferimento a: https://github.com/OData/WebApi/blob/master/OData/test/UnitTest/System.Web.OData.Test/OData/Query/ODataQueryContextTests.cs#L181- L200 –
Grazie Sam, ci proverò .. Non usato per MS con open source ... –
Sam. Sei fantastico! Se lo metti in una risposta, posso accettarlo. Grazie mille. Ha funzionato come un fascino. –