Collection Exists Criteria in WCF Data Services
Asked Answered
B

3

6

I'm trying to ask my OData service:

"Give me all the Products that do not have a Category"

with the Products and Category have a m2m relationship.

I've tried:

from p in Products
where p.Categories == null 
select p

and

from p in Products
where !p.Categories.Any() 
select p

and

from p in Products
where p.Categories.Count == 0
select p

but all of these give me not supported exceptions.


I'm not looking for alternatives or options. Please don't answer with other options.

Balch answered 2/8, 2010 at 18:41 Comment(0)
B
2

This is not supported:

http://social.msdn.microsoft.com/Forums/en-US/adodotnetdataservices/thread/b505d630-c808-4bde-b08e-3ce1dd17f621/

The OData URL query language currently doesn't support this type of query. As a result the client's LINQ processor doesn't support it either. If you think it's valuable to add such functionality please use our connect site to suggest the feature, it makes our planning job easier next time around. https://connect.microsoft.com/dataplatform/content/content.aspx?ContentID=15540&wa=wsignin1.0

As a workaround you could probably use service operation. Define a service operation which returns IQueryable (so that you can compose some more query operators on the result from the client) and use the server side provider to issues the query above.

Balch answered 4/8, 2010 at 13:18 Comment(0)
H
2

My experience with WCF Data Services is that the client subset of LINQ to rest is lacking.

My biased option would be to just move it to server side where you have access to the full implementation of LINQ to entites? (or whatever you are using to implement your Data Service).

 [WebGet]
 public IQueryable<Products> GetProductsWithoutCategories(){
    /*start psudo code
      from p in Products
         where p.Categories.Count == 0
      select p


    */

 }
Hypervitaminosis answered 2/8, 2010 at 19:27 Comment(1)
The client indeed doesn't support this type of query, because the URL query language doesn't support it. The solution with a service operation as posted above is a good one though.Packard
B
2

This is not supported:

http://social.msdn.microsoft.com/Forums/en-US/adodotnetdataservices/thread/b505d630-c808-4bde-b08e-3ce1dd17f621/

The OData URL query language currently doesn't support this type of query. As a result the client's LINQ processor doesn't support it either. If you think it's valuable to add such functionality please use our connect site to suggest the feature, it makes our planning job easier next time around. https://connect.microsoft.com/dataplatform/content/content.aspx?ContentID=15540&wa=wsignin1.0

As a workaround you could probably use service operation. Define a service operation which returns IQueryable (so that you can compose some more query operators on the result from the client) and use the server side provider to issues the query above.

Balch answered 4/8, 2010 at 13:18 Comment(0)
S
0

My solution to this problem was to $expand the related field in the query, and then test if that field was empty or not...

   JArray _array = (JArray)main_table_object["some_related_field"];

   if (_array.Count > 0) 
      continue; 

.. this is in the context of queries from Windows 7 phone to a WCF service using JSON as the message format.

Sedation answered 25/8, 2011 at 18:54 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.