Question

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).

Was it helpful?

Solution

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.

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top