本文整理汇总了C#中OrderByQueryOption类的典型用法代码示例。如果您正苦于以下问题:C# OrderByQueryOption类的具体用法?C# OrderByQueryOption怎么用?C# OrderByQueryOption使用的例子?那么恭喜您, 这里精选的类代码示例或许可以为您提供帮助。
OrderByQueryOption类属于命名空间,在下文中一共展示了OrderByQueryOption类的20个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于我们的系统推荐出更棒的C#代码示例。
示例1: BindOrderByQueryOption
public static string BindOrderByQueryOption(OrderByQueryOption orderByQuery)
{
StringBuilder sb = new StringBuilder();
if (orderByQuery != null)
{
sb.Append("order by ");
foreach (var orderByNode in orderByQuery.OrderByNodes)
{
var orderByPropertyNode = orderByNode as OrderByPropertyNode;
if (orderByPropertyNode != null)
{
sb.Append(orderByPropertyNode.Property.Name);
sb.Append(orderByPropertyNode.Direction == OrderByDirection.Ascending ? " asc," : " desc,");
}
else
{
throw new ODataException("Only ordering by properties is supported");
}
}
}
if (sb[sb.Length - 1] == ',')
{
sb.Remove(sb.Length - 1, 1);
}
return sb.ToString();
}
开发者ID:wk-j,项目名称:asp-samples,代码行数:30,代码来源:NHibernateOrderByBinder.cs
示例2: Validate
public virtual void Validate(OrderByQueryOption orderByOption, ODataValidationSettings validationSettings)
{
if (orderByOption == null)
{
throw Error.ArgumentNull("orderByOption");
}
if (validationSettings == null)
{
throw Error.ArgumentNull("validationSettings");
}
if (validationSettings.AllowedOrderByProperties.Count > 0)
{
ICollection<OrderByPropertyNode> propertyNodes = orderByOption.PropertyNodes;
foreach (OrderByPropertyNode property in propertyNodes)
{
if (!validationSettings.AllowedOrderByProperties.Contains(property.Property.Name))
{
throw new ODataException(Error.Format(SRResources.NotAllowedOrderByProperty, property.Property.Name, "AllowedOrderByProperties"));
}
}
}
}
开发者ID:Swethach,项目名称:aspnetwebstack,代码行数:25,代码来源:OrderByQueryValidator.cs
示例3: ODataQueryOptions
/// <summary>
/// Initializes a new instance of the <see cref="ODataQueryOptions"/> class based on the incoming request and some metadata information from
/// the <see cref="ODataQueryContext"/>.
/// </summary>
/// <param name="context">The <see cref="ODataQueryContext"/> which contains the <see cref="IEdmModel"/> and some type information</param>
/// <param name="request">The incoming request message</param>
public ODataQueryOptions(ODataQueryContext context, HttpRequestMessage request)
{
if (context == null)
{
throw Error.ArgumentNull("context");
}
if (request == null)
{
throw Error.ArgumentNull("request");
}
// remember the context
Context = context;
// Parse the query from request Uri
RawValues = new ODataRawQueryOptions();
IEnumerable<KeyValuePair<string, string>> queryParameters = request.GetQueryNameValuePairs();
foreach (KeyValuePair<string, string> kvp in queryParameters)
{
switch (kvp.Key)
{
case "$filter":
RawValues.Filter = kvp.Value;
ThrowIfEmpty(kvp.Value, "$filter");
Filter = new FilterQueryOption(kvp.Value, context);
break;
case "$orderby":
RawValues.OrderBy = kvp.Value;
ThrowIfEmpty(kvp.Value, "$orderby");
OrderBy = new OrderByQueryOption(kvp.Value, context);
break;
case "$top":
RawValues.Top = kvp.Value;
ThrowIfEmpty(kvp.Value, "$top");
Top = new TopQueryOption(kvp.Value, context);
break;
case "$skip":
RawValues.Skip = kvp.Value;
ThrowIfEmpty(kvp.Value, "$skip");
Skip = new SkipQueryOption(kvp.Value, context);
break;
case "$select":
RawValues.Select = kvp.Value;
break;
case "$inlinecount":
RawValues.InlineCount = kvp.Value;
break;
case "$expand":
RawValues.Expand = kvp.Value;
break;
case "$skiptoken":
RawValues.SkipToken = kvp.Value;
break;
default:
// we don't throw if we can't recognize the query
break;
}
}
}
开发者ID:chrisortman,项目名称:aspnetwebstack,代码行数:66,代码来源:ODataQueryOptions.cs
示例4: ValidateAllowsOrderByIt
public void ValidateAllowsOrderByIt()
{
// Arrange
OrderByQueryOption option = new OrderByQueryOption("$it", new ODataQueryContext(EdmCoreModel.Instance, typeof(int)));
ODataValidationSettings settings = new ODataValidationSettings();
// Act & Assert
Assert.DoesNotThrow(() => _validator.Validate(option, settings));
}
开发者ID:sujiantao,项目名称:aspnetwebstack,代码行数:9,代码来源:OrderByQueryValidatorTest.cs
示例5: ValidateAllowsOrderByIt_IfExplicitlySpecified
public void ValidateAllowsOrderByIt_IfExplicitlySpecified()
{
// Arrange
OrderByQueryOption option = new OrderByQueryOption("$it", new ODataQueryContext(EdmCoreModel.Instance, typeof(int)), queryTranslator: null);
ODataValidationSettings settings = new ODataValidationSettings { AllowedOrderByProperties = { "$it" } };
// Act & Assert
Assert.DoesNotThrow(() => _validator.Validate(option, settings));
}
开发者ID:xuanvufs,项目名称:WebApi,代码行数:9,代码来源:OrderByQueryValidatorTest.cs
示例6: ApplyInValidOrderbyQueryThrows
public void ApplyInValidOrderbyQueryThrows(string orderbyValue)
{
var model = new ODataModelBuilder().Add_Customer_EntityType().Add_Customers_EntitySet().GetEdmModel();
var context = new ODataQueryContext(model, typeof(Customer));
var orderby = new OrderByQueryOption(orderbyValue, context);
Assert.Throws<ODataException>(() =>
orderby.ApplyTo(ODataQueryOptionTest.Customers));
}
开发者ID:quentez,项目名称:aspnetwebstack,代码行数:9,代码来源:OrderByQueryOptionTest.cs
示例7: CanConstructValidFilterQuery
public void CanConstructValidFilterQuery(string orderbyValue)
{
var model = new ODataModelBuilder().Add_Customer_EntityType().Add_Customers_EntitySet().GetEdmModel();
var context = new ODataQueryContext(model, typeof(Customer));
var orderby = new OrderByQueryOption(orderbyValue, context);
Assert.Same(context, orderby.Context);
Assert.Equal(orderbyValue, orderby.RawValue);
}
开发者ID:ZhaoYngTest01,项目名称:WebApi,代码行数:9,代码来源:OrderByQueryOptionTest.cs
示例8: ValidateAllowsOrderByIt
public void ValidateAllowsOrderByIt()
{
// Arrange
OrderByQueryOption option = new OrderByQueryOption("$it", _context);
ODataValidationSettings settings = new ODataValidationSettings();
// Act & Assert
Assert.DoesNotThrow(() => _validator.Validate(option, settings));
}
开发者ID:naulizzang,项目名称:aspnetwebstack,代码行数:9,代码来源:OrderByQueryValidatorTest.cs
示例9: ValidateWillAllowId
public void ValidateWillAllowId()
{
// Arrange
OrderByQueryOption option = new OrderByQueryOption("Id", _context);
ODataValidationSettings settings = new ODataValidationSettings();
settings.AllowedOrderByProperties.Add("Id");
// Act & Assert
Assert.DoesNotThrow(() => _validator.Validate(option, settings));
}
开发者ID:naulizzang,项目名称:aspnetwebstack,代码行数:10,代码来源:OrderByQueryValidatorTest.cs
示例10: Validate
// Disallow the 'desc' parameter for $orderby option.
public override void Validate(OrderByQueryOption orderByOption,
ODataValidationSettings validationSettings)
{
if (orderByOption.OrderByNodes.Any(
node => node.Direction == OrderByDirection.Descending))
{
throw new ODataException("The 'desc' option is not supported.");
}
base.Validate(orderByOption, validationSettings);
}
开发者ID:syfbme,项目名称:CDMISrestful,代码行数:11,代码来源:QueryValidation.cs
示例11: ValidateWillNotAllowName
public void ValidateWillNotAllowName()
{
// Arrange
OrderByQueryOption option = new OrderByQueryOption("Name", _context);
ODataValidationSettings settings = new ODataValidationSettings();
settings.AllowedOrderByProperties.Add("Id");
// Act & Assert
Assert.Throws<ODataException>(() => _validator.Validate(option, settings),
"Order by 'Name' is not allowed. To allow it, set the 'AllowedOrderByProperties' property on QueryableAttribute or QueryValidationSettings.");
}
开发者ID:naulizzang,项目名称:aspnetwebstack,代码行数:11,代码来源:OrderByQueryValidatorTest.cs
示例12: ValidateDisallowsOrderByIt_IfTurnedOff
public void ValidateDisallowsOrderByIt_IfTurnedOff()
{
// Arrange
_context = new ODataQueryContext(EdmCoreModel.Instance, typeof(int));
OrderByQueryOption option = new OrderByQueryOption("$it", _context, queryTranslator: null);
ODataValidationSettings settings = new ODataValidationSettings();
settings.AllowedOrderByProperties.Add("dummy");
// Act & Assert
Assert.Throws<ODataException>(
() => _validator.Validate(option, settings),
"Order by '$it' is not allowed. To allow it, set the 'AllowedOrderByProperties' property on EnableQueryAttribute or QueryValidationSettings.");
}
开发者ID:xuanvufs,项目名称:WebApi,代码行数:13,代码来源:OrderByQueryValidatorTest.cs
示例13: BuildCustomRequest
public static HttpRequestMessage BuildCustomRequest(this ODataQueryOptions originalOptions,
out TopQueryOption top,
out SkipQueryOption skip, out OrderByQueryOption orderBy)
{
top = null;
skip = null;
orderBy = null;
// cuttin out $top and $skip from the request
HttpRequestMessage customRequest = originalOptions.Request;
if (customRequest.Properties.ContainsKey(HttpPropertyKeys.RequestQueryNameValuePairsKey))
{
Uri uri = originalOptions.Request.RequestUri;
var pairs =
customRequest.Properties[HttpPropertyKeys.RequestQueryNameValuePairsKey] as
IEnumerable<KeyValuePair<string, string>>;
if (pairs != null)
{
IEnumerable<KeyValuePair<string, string>> jQueryNameValuePairs =
new FormDataCollection(uri).GetJQueryNameValuePairs();
var updatedPairs = new List<KeyValuePair<string, string>>();
foreach (var pair in jQueryNameValuePairs)
{
if (pair.Key.Equals("$top"))
{
top = originalOptions.Top;
}
else if (pair.Key.Equals("$skip"))
{
skip = originalOptions.Skip;
}
else if (pair.Key.Equals("$orderby"))
{
orderBy = originalOptions.OrderBy;
}
else
{
updatedPairs.Add(pair);
}
}
customRequest.Properties.Remove(HttpPropertyKeys.RequestQueryNameValuePairsKey);
customRequest.Properties.Add(HttpPropertyKeys.RequestQueryNameValuePairsKey, updatedPairs);
}
}
return customRequest;
}
开发者ID:alexlapinski,项目名称:autohaus,代码行数:51,代码来源:ODataHelper.cs
示例14: ValidateWillNotAllowMultipleProperties
public void ValidateWillNotAllowMultipleProperties()
{
// Arrange
OrderByQueryOption option = new OrderByQueryOption("Name desc, Id asc", _context);
ODataValidationSettings settings = new ODataValidationSettings();
Assert.DoesNotThrow(() => _validator.Validate(option, settings));
settings.AllowedOrderByProperties.Add("Address");
settings.AllowedOrderByProperties.Add("Name");
// Act & Assert
Assert.Throws<ODataException>(() => _validator.Validate(option, settings),
"Order by 'Id' is not allowed. To allow it, set the 'AllowedOrderByProperties' property on QueryableAttribute or QueryValidationSettings.");
}
开发者ID:naulizzang,项目名称:aspnetwebstack,代码行数:14,代码来源:OrderByQueryValidatorTest.cs
示例15: GetQueryNodeParsesQuery
public void GetQueryNodeParsesQuery()
{
var model = new ODataModelBuilder().Add_Customer_EntityType().Add_Customers_EntitySet().GetEdmModel();
var context = new ODataQueryContext(model, typeof(Customer), "Customers");
var orderby = new OrderByQueryOption("Name,Website", context);
var node = orderby.QueryNode;
Assert.Equal(QueryNodeKind.PropertyAccess, node.Expression.Kind);
var websiteNode = node.Expression as PropertyAccessQueryNode;
Assert.Equal("Website", websiteNode.Property.Name);
var nameNode = ((OrderByQueryNode)node.Collection).Expression;
Assert.Equal(QueryNodeKind.PropertyAccess, nameNode.Kind);
Assert.Equal("Name", ((PropertyAccessQueryNode)nameNode).Property.Name);
}
开发者ID:chrisortman,项目名称:aspnetwebstack,代码行数:15,代码来源:OrderByQueryOptionTest.cs
示例16: ApplyTo_NestedProperties_DoesNotHandleNullPropagation_IfExplicitInSettings
public void ApplyTo_NestedProperties_DoesNotHandleNullPropagation_IfExplicitInSettings()
{
// Arrange
var model = new ODataModelBuilder().Add_Customer_EntityType_With_Address().Add_Customers_EntitySet().GetServiceModel();
var orderByOption = new OrderByQueryOption("Address/City asc", new ODataQueryContext(model, typeof(Customer)), null);
var customers = (new List<Customer>{
new Customer { CustomerId = 1, Address = null },
new Customer { CustomerId = 2, Address = new Address { City = "B" } },
new Customer { CustomerId = 3, Address = new Address { City = "A" } }
}).AsQueryable();
ODataQuerySettings settings = new ODataQuerySettings { HandleNullPropagation = HandleNullPropagationOption.False };
// Act & Assert
Assert.Throws<NullReferenceException>(() => orderByOption.ApplyTo(customers, settings).ToArray());
}
开发者ID:xuanvufs,项目名称:WebApi,代码行数:16,代码来源:OrderByQueryOptionTest.cs
示例17: PropertyNodes_Getter_Parses_Query
public void PropertyNodes_Getter_Parses_Query()
{
// Arrange
var model = new ODataModelBuilder().Add_Customer_EntityType().Add_Customers_EntitySet().GetEdmModel();
var context = new ODataQueryContext(model, typeof(Customer), "Customers");
var orderby = new OrderByQueryOption("Name,Website", context);
// Act
ICollection<OrderByPropertyNode> nodes = orderby.PropertyNodes;
// Assert
Assert.NotNull(nodes);
Assert.Equal(2, nodes.Count);
Assert.Equal("Name", nodes.First().Property.Name);
Assert.Equal("Website", nodes.Last().Property.Name);
}
开发者ID:chrissimon-au,项目名称:aspnetwebstack,代码行数:16,代码来源:OrderByQueryOptionTest.cs
示例18: PropertyNodes_Getter_Parses_Query
public void PropertyNodes_Getter_Parses_Query()
{
// Arrange
var model = new ODataModelBuilder().Add_Customer_EntityType().Add_Customers_EntitySet().GetEdmModel();
var context = new ODataQueryContext(model, typeof(Customer));
var orderby = new OrderByQueryOption("Name,Website", context);
ICollection<OrderByNode> nodes = orderby.OrderByNodes;
// Assert
Assert.False(nodes.OfType<OrderByItNode>().Any());
IEnumerable<OrderByPropertyNode> propertyNodes = nodes.OfType<OrderByPropertyNode>();
Assert.NotNull(propertyNodes);
Assert.Equal(2, propertyNodes.Count());
Assert.Equal("Name", propertyNodes.First().Property.Name);
Assert.Equal("Website", propertyNodes.Last().Property.Name);
}
开发者ID:ZhaoYngTest01,项目名称:WebApi,代码行数:17,代码来源:OrderByQueryOptionTest.cs
示例19: Validate
/// <summary>
/// Validates an <see cref="OrderByQueryOption" />.
/// </summary>
/// <param name="orderByOption">The $orderby query.</param>
/// <param name="validationSettings">The validation settings.</param>
public virtual void Validate(OrderByQueryOption orderByOption, ODataValidationSettings validationSettings)
{
if (orderByOption == null)
{
throw Error.ArgumentNull("orderByOption");
}
if (validationSettings == null)
{
throw Error.ArgumentNull("validationSettings");
}
int nodeCount = 0;
for (OrderByClause clause = orderByOption.OrderByClause; clause != null; clause = clause.ThenBy)
{
nodeCount++;
if (nodeCount > validationSettings.MaxOrderByNodeCount)
{
throw new ODataException(Error.Format(SRResources.OrderByNodeCountExceeded, validationSettings.MaxOrderByNodeCount));
}
}
if (validationSettings.AllowedOrderByProperties.Count > 0)
{
IEnumerable<OrderByNode> orderByNodes = orderByOption.OrderByNodes;
foreach (OrderByNode node in orderByNodes)
{
string propertyName = null;
OrderByPropertyNode property = node as OrderByPropertyNode;
if (property != null)
{
propertyName = property.Property.Name;
}
else if ((node as OrderByItNode) != null && !validationSettings.AllowedOrderByProperties.Contains("$it"))
{
propertyName = "$it";
}
if (propertyName != null && !validationSettings.AllowedOrderByProperties.Contains(propertyName))
{
throw new ODataException(Error.Format(SRResources.NotAllowedOrderByProperty, propertyName, "AllowedOrderByProperties"));
}
}
}
}
开发者ID:ZhaoYngTest01,项目名称:WebApi,代码行数:51,代码来源:OrderByQueryValidator.cs
示例20: CanApplySkipOrderby
public void CanApplySkipOrderby()
{
var model = new ODataModelBuilder().Add_Customer_EntityType().Add_Customers_EntitySet().GetServiceModel();
var context = new ODataQueryContext(model, typeof(Customer));
var orderbyOption = new OrderByQueryOption("Name", context, queryTranslator: null);
var skipOption = new SkipQueryOption("1", context);
var customers = (new List<Customer>{
new Customer { CustomerId = 1, Name = "Andy" },
new Customer { CustomerId = 2, Name = "Aaron" },
new Customer { CustomerId = 3, Name = "Alex" }
}).AsQueryable();
IQueryable queryable = orderbyOption.ApplyTo(customers);
queryable = skipOption.ApplyTo(queryable, new ODataQuerySettings());
var results = ((IQueryable<Customer>)queryable).ToArray();
Assert.Equal(2, results.Length);
Assert.Equal(3, results[0].CustomerId);
Assert.Equal(1, results[1].CustomerId);
}
开发者ID:xuanvufs,项目名称:WebApi,代码行数:20,代码来源:SkipQueryOptionTests.cs
注:本文中的OrderByQueryOption类示例整理自Github/MSDocs等源码及文档管理平台,相关代码片段筛选自各路编程大神贡献的开源项目,源码版权归原作者所有,传播和使用请参考对应项目的License;未经允许,请勿转载。 |
请发表评论