Skip to main content
.NET 6.0+

How to: Use Function Criteria Operators to Filter List Views

  • 3 minutes to read

The XAF provides various approaches to filter List Views: on data source level, via the Application Model and on the UI specific level. In each of these approaches, you may need to set static variables as filter criteria values. For example, the filter “Task.DueDate must be set to the current date” needs the CurrentDate variable, calculated every time it is required. For this purpose, the use Function Criteria Operators. They represent functions you can use in criteria. In this topic, you will learn how to use these Function Criteria Operators when setting filter criteria for the Task List View. To see a full list of built-in Function Criteria Operators and learn how to implement custom ones, refer to the Function Criteria Operators topic.

Since the technique for using Function Criteria Operators is common to any filtering approach, both in code and the Model Editor, the ListViewFilterAttribute will be chosen for demonstration.

Note

Before reviewing the example, be sure to read the “Important Remark on the DateTime Function Criteria Operators” section of the Function Criteria Operators topic.

The following code demonstrates how to implement various filters using the LocalDateTimeToday, LocalDateTimeLastWeek and LocalDateTimeThisWeek Function Criteria Operators:

using DevExpress.ExpressApp.Model;
using DevExpress.ExpressApp.SystemModule;
//...
[DefaultClassOptions]
[ListViewFilter("Today", "GetDate([DueDate]) = LocalDateTimeToday()")]
[ListViewFilter("In three days", @"[DueDate] >= ADDDAYS(LocalDateTimeToday(), -3) AND 
    [DueDate] < LocalDateTimeToday()")]
[ListViewFilter("In two weeks", @"[DueDate] >= ADDDAYS(LocalDateTimeToday(), -14) AND 
    [DueDate] < LocalDateTimeToday()")]
[ListViewFilter("The last week", @"GetDate([DueDate]) > LocalDateTimeLastWeek() AND 
    GetDate([DueDate]) <= ADDDAYS(LocalDateTimeLastWeek(), 5)")]
[ListViewFilter("This week", @"GetDate([DueDate]) > LocalDateTimeThisWeek() AND 
    GetDate([DueDate]) <= ADDDAYS(LocalDateTimeThisWeek(), 5)")]
public class Task : BaseObject {
    [ModelDefault("EditMask","d")]
    public virtual DateTime DueDate { get; set; }
}

// Make sure that you use options.UseChangeTrackingProxies() in your DbContext settings.

The code above generates several Filter child nodes for the Application Model‘s Views | Task_ListView node:

FCO_ListViews

Note

You could generate these nodes manually in the Model Editor.

The following image demonstrates the Filter Action that contains all the filters provided by the code above:

Read-OnlyParameters for List Views_Result