本文整理汇总了C#中ODataQueryOptions.ApplyTo方法的典型用法代码示例。如果您正苦于以下问题:C# ODataQueryOptions.ApplyTo方法的具体用法?C# ODataQueryOptions.ApplyTo怎么用?C# ODataQueryOptions.ApplyTo使用的例子?那么, 这里精选的方法代码示例或许可以为您提供帮助。您也可以进一步了解该方法所在类ODataQueryOptions
的用法示例。
在下文中一共展示了ODataQueryOptions.ApplyTo方法的15个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的C#代码示例。
示例1: GetTeams
public IQueryable<Team> GetTeams(ODataQueryOptions queryOptions)
{
// Validate query options
var settings = new ODataValidationSettings()
{
MaxTop = 400
};
queryOptions.Validate(settings);
// Apply the filter before going through to check if links exist for significant performance improvements
var teams = (IQueryable<Team>)queryOptions.ApplyTo(Db.core.Teams);
// RouteLinker creates Uris for actions
var linker = new RouteLinker(Request);
foreach (var team in teams)
{
if (team.Links == null)
{
team.Links = new SerializableDynamic();
team.Links.url = linker.GetUri<TeamsController>(c => c.GetTeam(team.Number)).ToString();
}
}
var nextRequest = Request.RequestUri;
return Db.core.Teams.AsQueryable();
}
示例2: Get
public IHttpActionResult Get(ODataQueryOptions<ODataQueryOptionTest_EntityModel> queryOptions)
{
// Don't apply Filter and Expand, but apply Select.
var appliedQueryOptions = AllowedQueryOptions.Skip | AllowedQueryOptions.Filter | AllowedQueryOptions.Expand;
var res = queryOptions.ApplyTo(_entityModels, appliedQueryOptions);
return Ok(res.AsQueryable());
}
示例3: GetFromManager
// Pass ODataQueryOptions as parameter, and call validation manually
public IHttpActionResult GetFromManager(ODataQueryOptions<Manager> queryOptions)
{
if (queryOptions.SelectExpand != null)
{
queryOptions.SelectExpand.LevelsMaxLiteralExpansionDepth = 5;
}
var validationSettings = new ODataValidationSettings { MaxExpansionDepth = 5 };
try
{
queryOptions.Validate(validationSettings);
}
catch (ODataException e)
{
var responseMessage = new HttpResponseMessage(HttpStatusCode.BadRequest);
responseMessage.Content = new StringContent(
string.Format("The query specified in the URI is not valid. {0}", e.Message));
return ResponseMessage(responseMessage);
}
var querySettings = new ODataQuerySettings();
var result = queryOptions.ApplyTo(_employees.OfType<Manager>().AsQueryable(), querySettings).AsQueryable();
return Ok(result, result.GetType());
}
示例4: GetCustomers
public IEnumerable<Customer> GetCustomers(ODataQueryOptions<Customer> queryOptions)
{
// validate the query.
queryOptions.Validate(_validationSettings);
// Apply the query.
IQuery query = queryOptions.ApplyTo(_db);
Console.WriteLine("Executing HQL:\t" + query);
Console.WriteLine();
return query.List<Customer>();
}
示例5: GetImages
// GET: odata/Images
public PageResult<Image> GetImages(ODataQueryOptions<Image> options)
{
ODataQuerySettings settings = new ODataQuerySettings()
{
PageSize = CollectionOfWorkers.SizeOfPage
};
IQueryable results = options.ApplyTo(db.Images.AsQueryable(), settings);
return new PageResult<Image>(
results as IEnumerable<Image>,
null,
db.Images.Count());
}
示例6: Get
public IHttpActionResult Get(ODataQueryOptions<AttributedSelectExpandCustomer> options)
{
IQueryable result = options.ApplyTo(Enumerable.Range(1, 10).Select(i => new AttributedSelectExpandCustomer
{
Id = i,
Name = "Name " + i,
Orders = Enumerable.Range(1, 10).Select(j => new AttributedSelectExpandOrder
{
CustomerName = "Customer Name" + j,
Id = j,
Total = i * j
}).ToList()
}).AsQueryable());
return Ok(result);
}
示例7: Get
// GET: Domains
public IEnumerable<DomainsProjection> Get(ODataQueryOptions<DomainsProjection> options)
{
var querySettings = new ODataQuerySettings()
{
EnableConstantParameterization = true,
EnsureStableOrdering = true
};
var raw = GetDomains().OrderBy(d => d.HostName);
var total = options.Filter == null ? raw.Count() : options.Filter.ApplyTo(raw, querySettings).Count();
var res = (IQueryable<DomainsProjection>) options.ApplyTo(raw, querySettings);
return new PageResult<DomainsProjection>(res, options.GetNextLink(total), total);
//return GetDomains().ToArray();
}
示例8: Get
// Note this can be done through Queryable attribute as well
public IQueryable<Order> Get(ODataQueryOptions queryOptions)
{
// Register a custom FilterByValidator to disallow custom logic in the filter query
if (queryOptions.Filter != null)
{
queryOptions.Filter.Validator = new RestrictiveFilterByQueryValidator();
}
// Validate the query, we only allow order by Id property and
// we only allow maximum Top query value to be 9
ODataValidationSettings settings = new ODataValidationSettings() { MaxTop = 9 };
settings.AllowedOrderByProperties.Add("Id");
queryOptions.Validate(settings);
// Apply the query
return queryOptions.ApplyTo(OrderList.AsQueryable()) as IQueryable<Order>;
}
示例9: FindJobs
internal async Task<QueryResult<Job>> FindJobs(ODataQueryOptions<Job> query, int start, int limit)
{
return await Task.Run(() =>
{
IQueryable queryResult;
queryResult = query.ApplyTo(_context.Jobs.AsQueryable(), AllowedQueryOptions.OrderBy);
if (query.OrderBy != null)
queryResult = queryResult.LinqToQuerystring(typeof(Job), "$orderby=" + query.OrderBy.RawValue) as IQueryable<Job>;
var data = queryResult as IEnumerable<Job>;
if (query.Count != null && query.Count.Value)
return new QueryResult<Job>(null, data.LongCount());
else
return new QueryResult<Job>(data.Skip(start).Take(limit), data.LongCount());
});
}
示例10: Get
public IHttpActionResult Get(ODataQueryOptions<DLManager> queryOptions)
{
ODataValidationSettings settings = new ODataValidationSettings();
settings.MaxExpansionDepth = 1;
try
{
queryOptions.Validate(settings);
}
catch (ODataException e)
{
var responseMessage = new HttpResponseMessage(HttpStatusCode.BadRequest);
responseMessage.Content = new StringContent(
String.Format("The query specified in the URI is not valid. {0}", e.Message));
return ResponseMessage(responseMessage);
}
var managers = queryOptions.ApplyTo(_DLManagers.AsQueryable()).AsQueryable();
return Ok(managers, managers.GetType());
}
示例11: GetRecipeAsync
// GET: api/Recipes
public async Task<IQueryable<Recipe>> GetRecipeAsync(ODataQueryOptions options)
{
var appUser = await AuthRepo.FindUserByName(User.Identity.Name);
IQueryable results = options.ApplyTo(MatsMatRepo.GetAllRecipes());
//IQueryable results = MatsMatRepo.GetAllRecipes();
var result = results as IQueryable<Recipe>;
var paginationHeader = new
{
TotalCount = Request.ODataProperties().TotalCount,
Skip = options?.Skip?.Value ?? 0,
Top = options?.Top?.Value ?? 0,
Url = ""
};
//Add pagination info to response header
System.Web.HttpContext.Current.Response.AddHeader("Access-Control-Expose-Headers", "X-Pagination");
System.Web.HttpContext.Current.Response.Headers.Add("X-Pagination", Newtonsoft.Json.JsonConvert.SerializeObject(paginationHeader));
return result;
}
示例12: ApplyTo_Entity_ThrowsInvalidOperation_IfNonSelectExpand
public void ApplyTo_Entity_ThrowsInvalidOperation_IfNonSelectExpand(string parameter)
{
CustomersModelWithInheritance model = new CustomersModelWithInheritance();
model.Model.SetAnnotationValue(model.Customer, new ClrTypeAnnotation(typeof(Customer)));
HttpRequestMessage request = new HttpRequestMessage(HttpMethod.Get, "http://localhost?" + parameter);
ODataQueryContext context = new ODataQueryContext(model.Model, typeof(Customer));
ODataQueryOptions queryOptions = new ODataQueryOptions(context, request);
Assert.Throws<InvalidOperationException>(
() => queryOptions.ApplyTo(42, new ODataQuerySettings()),
"The requested resource is not a collection. Query options $filter, $orderby, $count, $skip, and $top can be applied only on collections.");
}
示例13: ApplyTo_Entity_ThrowsArgumentNull_QuerySettings
public void ApplyTo_Entity_ThrowsArgumentNull_QuerySettings()
{
ODataQueryContext context = new ODataQueryContext(EdmCoreModel.Instance, typeof(int));
ODataQueryOptions queryOptions = new ODataQueryOptions(context, new HttpRequestMessage());
Assert.ThrowsArgumentNull(
() => queryOptions.ApplyTo(entity: 42, querySettings: null),
"querySettings");
}
示例14: ApplyTo_IgnoresCount_IfRequestAlreadyHasCount
public void ApplyTo_IgnoresCount_IfRequestAlreadyHasCount()
{
// Arrange
long count = 42;
HttpRequestMessage request = new HttpRequestMessage(HttpMethod.Get, "http://localhost/?$count=true");
ODataQueryContext context = new ODataQueryContext(EdmCoreModel.Instance, typeof(int));
ODataQueryOptions options = new ODataQueryOptions(context, request);
request.ODataProperties().TotalCount = count;
// Act
options.ApplyTo(Enumerable.Empty<int>().AsQueryable());
// Assert
Assert.Equal(count, request.ODataProperties().TotalCount);
}
示例15: ODataQueryOptions_IgnoresUnknownOperatorStartingWithDollar
public void ODataQueryOptions_IgnoresUnknownOperatorStartingWithDollar()
{
HttpRequestMessage request = new HttpRequestMessage(HttpMethod.Get, "http://localhost/?$filter=$it eq 6&$unknown=value");
ODataQueryContext context = new ODataQueryContext(EdmCoreModel.Instance, typeof(int));
ODataQueryOptions options = new ODataQueryOptions(context, request);
var queryable = options.ApplyTo(Enumerable.Range(0, 10).AsQueryable());
IEnumerator enumerator = queryable.GetEnumerator();
Assert.True(enumerator.MoveNext());
Assert.Equal(6, enumerator.Current);
}