Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

OData uri for filter collection empty or any eq 4

Using OData, how can I make this filter?

My class:

public class Aviso
{
    public int Id { get; set; }
    public virtual ICollection<User> Destinatarios { get; set; }
    public string Url { get; set; }
}

THE uri attempt: /odata/avisos?$filter=(Destinatarios eq null or Destinatarios/count eq 0 or Destinatarios/any(it:it/Id eq 4) )

The goal is to return where any Destinatarios are 4 or collection is empty (or null).

like image 434
ridermansb Avatar asked Sep 20 '13 13:09

ridermansb


People also ask

What is Uri in OData?

The Open Data Protocol (OData) enables the creation of REST-based data services, which allow resources, identified using Uniform Resource Identifiers (URIs) and defined in a data model, to be published and edited by Web clients using simple HTTP messages.

How do I filter OData query?

You can use filter expressions in OData requests to filter and return only those results that match the expressions specified. You do this by adding the $filter system query option to the end of the OData request.

How do I find OData URL?

If you have an on-premise system, then call /N/IWFND/MAINT_SERVICE, choose your service, click on Gateway Client, set the protocol to HTTPS and you'll see the full URL. If you don't then HTTPS isn't configured for your system --> call SMICM, Goto (from menu), Services and check the Host and Port columns in HTTP row.

What is $value in OData?

The $value option is used to get individual properties of an Entity. There are two ways to get individual properties from an entity. We can get the response in either OData format or get the raw value of the property. We need to add method to the controller named GetProperty here property is a name of the property.


1 Answers

Try,

/odata/avisos?$filter=not Destinatarios/any() or Destinatarios/any(d: d/Id eq 4)

You do not need the null check as we handle null propagation for you. Also, having null collections is not a good practice in general as it causes confusion between null and empty collections.

like image 193
RaghuRam Nadiminti Avatar answered Sep 19 '22 15:09

RaghuRam Nadiminti