Vaccano
Vaccano

Reputation: 82467

OData query that uses .IncludeTotalCount() on a filtered (where clause) query

Say I have the following query on the Netflix OData Endpoint:

Titles.Where(x=>x.AverageRating > 3.0)

Is there a way to use .IncludeTotalCount() to get the count of titles that fit the where clause? (Not the total count of items in the Titles resource.)

I looked at the code provided here: http://msdn.microsoft.com/en-us/library/ee474390.aspx

But it does not seem to work with a where clause.

Upvotes: 1

Views: 1890

Answers (2)

Chris Liechty
Chris Liechty

Reputation: 60

Based on @Vitek Karas' response and your comments, it looks like you need to cast the IQueryable you get back from the where clause to DataServiceQuery.

Upvotes: 2

Vitek Karas MSFT
Vitek Karas MSFT

Reputation: 13320

I tried this and it works just fine:

DemoService ctx = new DemoService(new Uri("http://services.odata.org/OData/OData.svc/"));
var query = (DataServiceQuery<Product>)ctx.Products.IncludeTotalCount().Where(p => p.Rating < 4);
QueryOperationResponse<Product> response = (QueryOperationResponse<Product>)query.Execute();
Console.WriteLine(response.TotalCount);
foreach (var i in response)
{
    Console.WriteLine(i.Name + ", " + i.Rating.ToString());
}

What does it mean "it does not work" in your case?

Upvotes: 7

Related Questions