This content is archived. See latest version here

Last updated: Sep 21 2015

Introduction

The Filter method can be used to filter enum and nullable enum fields in a couple of ways. Below is a list of use cases and examples illustrating the available methods. Note that there's currently only support for filtering by exact matching of values for enums. That is, bitwise comparison for enum types that have the Flags attribute is not baked into the Match method but has to be done explicitly.

Matching by value

For exact matching we can use the Match method. The below search will match blog posts whose PublishState property is a specific enum value. The LINQ equivalent would be Where(x => x.PublishState == PublishState.Published).

C#
var searchQuery = client.Search<BlogPost>()
    .Filter(x => x.PublishState.Match(PublishState.Published));

Existance

To search for documents where an enum field has a value, we can use the Exists method. The below search will find blog posts that has an Approved property with a value. In other words, the below code is similar to the LINQ query Where(x => x.PublishState.HasValue).

C#
var searchQuery = client.Search<BlogPost>()
    .Filter(x => x.PublishState.Exists());

Do you have feedback on this documentation? Send an email to documentation@episerver.com. For development-related questions and discussions, refer to our Forums on https://world.episerver.com/forum/