Quantcast
Channel: ASPNETWebStack Issue Tracker Rss Feed
Viewing all articles
Browse latest Browse all 7215

Edited Issue: Can filter and order on relationships that are navigable [1598]

$
0
0
Setting IsNotNavigable() on a navigation property should also set IsNotFilterable() and IsUnsortable() however, if you define a model with customers, related orders and related credit score, and you make the Orders and CreditScore navigation properties not navigable, you can still filter and sort over the properties of the related entities using queries like this one:

/odata/QueryLimitCustomers?$filter=Orders/any(o: o/Id eq 5)
/odata/QueryLimitCustomers?$filter=CreditScore/CreditScore eq 100
/odata/QueryLimitCustomers?$orderby=CreditScore/CreditScore desc

```
private static IEdmModel GetModel()
{
ODataModelBuilder builder = new ODataConventionModelBuilder();
EntitySetConfiguration<QueryLimitCustomer> customers = builder.EntitySet<QueryLimitCustomer>("QueryLimitCustomers");
//Can limit sorting and filtering primitive properties
var name = customers.EntityType.Property(p => p.Name);
name.IsNonFilterable();
name.IsUnsortable();
//Can override the behavior specified by the attributes for primitive properties
customers.EntityType.Property(p => p.Age).IsFilterable().IsSortable();

//Can limit sorting and filtering for complex properties.
customers.EntityType.ComplexProperty(p => p.HomeAddress).IsNonFilterable().IsUnsortable();
//Can override the behavior specified by the attributes for complex properties.
customers.EntityType.ComplexProperty(p => p.BusinessAddress).IsFilterable().IsSortable();

ComplexTypeConfiguration<QueryLimitAddress> address = builder.ComplexType<QueryLimitAddress>();
//Can limit sorting and filtering for some of the properties of a complex type.
address.Property(p => p.Street).IsNonFilterable().IsUnsortable();
//Can override the behavior specified by the attributes on properties of a complex type.
address.Property(p => p.Country).IsFilterable().IsSortable();

//Can limit navigation of relationships
customers.EntityType.HasMany(c => c.Orders).IsNotNavigable().IsNotExpandable();
customers.EntityType.HasOptional(c => c.CreditScore).IsNotNavigable().IsNotExpandable();

//Can override the behavior specified by the attributes on a navigation property
customers.EntityType.HasMany(c => c.PossibleOrders).IsNavigable().IsExpandable();

//Can override the configuration set with attributes.
customers.EntityType.HasMany(c => c.PossibleOrders).IsNavigable().IsExpandable();
customers.EntityType.HasOptional(c => c.PastCreditScore).IsNavigable().IsExpandable();

EntitySetConfiguration<QueryLimitOrder> orders = builder.EntitySet<QueryLimitOrder>("QueryLimitOrders");
EntitySetConfiguration<QueryLimitOrderDetails> ordersdetails = builder.EntitySet<QueryLimitOrderDetails>("QueryLimitOrdersDetails");
EntitySetConfiguration<QueryLimitCreditScore> creditScores = builder.EntitySet<QueryLimitCreditScore>("QueryLimitCreditScores");
return builder.GetEdmModel();
}


public class QueryLimitCustomersController : ODataController
{
[Queryable(PageSize = 10, MaxExpansionDepth = 2)]
public IHttpActionResult Get()
{
return Ok(Enumerable.Range(0, 10).Select(i => new QueryLimitCustomer
{
Id = i,
Name = "Name " + i,
AlternativeAddress = new QueryLimitAddress
{
City = "City " + i,
Country = "Country " + i,
Street = "Street " + i,
ZipCode = i
},
BusinessAddress = new QueryLimitAddress
{
City = "City " + i,
Country = "Country " + i,
Street = "Street " + i,
ZipCode = i
},
HomeAddress = new QueryLimitAddress
{
City = "City " + i,
Country = "Country " + i,
Street = "Street " + i,
ZipCode = i
},
Orders = Enumerable.Range(0, i).Select(j => new QueryLimitOrder
{
Id = i * 10 + j,
PurchasedOn = DateTime.Parse("11/24/2013").Subtract(TimeSpan.FromDays(i * 10 + j))
}).ToList(),
PossibleOrders = Enumerable.Range(0, i).Select(j => new QueryLimitOrder
{
Id = i * 10 + j,
PurchasedOn = DateTime.Parse("11/24/2013").Subtract(TimeSpan.FromDays(i * 10 + j))
}).ToList(),
CreditScore = new QueryLimitCreditScore { Id = i, CreditScore = i * 50 }
}));
}
}

public class QueryLimitCustomer
{
public int Id { get; set; }
public string Name { get; set; }
public string MiddleName { get; set; }

[NonFilterable]
[Unsortable]
public string LastName { get; set; }

[NonFilterable]
[Unsortable]
public int Age { get; set; }

public QueryLimitAddress HomeAddress { get; set; }

[NonFilterable]
[Unsortable]
public QueryLimitAddress AlternativeAddress { get; set; }

[NonFilterable]
[Unsortable]
public QueryLimitAddress BusinessAddress { get; set; }
public ICollection<QueryLimitOrder> Orders { get; set; }

[NotExpandable]
[NotNavigable]
public ICollection<QueryLimitOrder> CompletedOrders { get; set; }

[NotExpandable]
[NotNavigable]
public ICollection<QueryLimitOrder> PossibleOrders { get; set; }
public QueryLimitCreditScore CreditScore { get; set; }
[NotExpandable]
[NotNavigable]
public QueryLimitCreditScore PastCreditScore { get; set; }
}

public class QueryLimitOrder
{
public int Id { get; set; }
public DateTime PurchasedOn { get; set; }
[NotNavigable]
[NotExpandable]
public ICollection<QueryLimitOrderDetails> Details { get; set; }
}

public class QueryLimitCreditScore
{
public int Id { get; set; }
public int CreditScore { get; set; }
}

public class QueryLimitOrderDetails
{
public int Id { get; set; }
public double Value { get; set; }
}

public class QueryLimitAddress
{
public string Street { get; set; }
public int ZipCode { get; set; }

[NonFilterable]
[Unsortable]
public string City { get; set; }

[NonFilterable]
[Unsortable]
public string Country { get; set; }
}

```

Viewing all articles
Browse latest Browse all 7215

Trending Articles



<script src="https://jsc.adskeeper.com/r/s/rssing.com.1596347.js" async> </script>