Progress will discontinue Telerik Platform on May 10th, 2018. Learn more

Filtering

Filtering

You can apply filters on any Create, Read, Update, and Delete data operations to alter the result of a query.

Types of filtering expressions

In the Backend Services .NET SDK there are three ways of defining filters wich work equally well and should be used by your Convenience.

  • LINQ expressions (Recomended) - this is the standard filtering expression for .NET, implemented over the data in Telerik Platform.
  • Condition classes - wrap the logic about most common filtering scenarios. It is suitable when LINQ just does not work for you.

    • ArrayCondition - whether or not an item is in array or all items in the array matches.
    • RegExCondition - wraps the logic about filtering with items by regular expression.

      Regular expressions support when filtering data from Data Connectors is limited to the following use cases. As a workaround, create a view or a stored procedure and map it to a Telerik Platform content type or cloud function respectively.

      • Starts with: ^searchstring
      • Ends with: searchstring$
      • Contains: .*searchstring.*

      Regular expression options are not supported when filtering data from Data Connectors. Do not specify any, otherwise you will receive an incorrect result.

    • ValueCondition - wraps the logic about comparing values.

    • WhereCondition - uses the same syntax as the $where operator in MongoDB.
    • CompoundCondition - allows you to chain conditions and apply "and" or "or" operators on them.
  • String expression - uses the same syntax as the $where operator in MongoDB. This theoretically is the slowest filtering because it does not use database indexes.

Retrieving by filter expression

In Telerik Platform you can read items by several filter expressions:

Value equal to

public async Task GetItemsWithText(EverliveApp app, string text)
{
    var activities = await app.WorkWith().Data<Activity>().Get().
   Where(activity => activity.Text == text).
   ExecuteAsync();

   foreach(var activity in activities)    
    {
        Debug.WriteLine("Activity: " + activity.Id);
    }
}

Value not equal to

public async Task GetActivities(EverliveApp app)
{
    var activities = await app.WorkWith().Data<Activity>().Get().
   Where(activity => activity.Text != "It is finally time for graduation! Good job everyone, we made it.!").
   ExecuteAsync();

   foreach(var activity in activities)    
    {
        Debug.WriteLine("Activity: " + activity.Id);
    }
}

Value compared to

By executing the following code snippet, you can get all items which value is greater than a certain constant:

public async Task GetRecentActivities(EverliveApp app)
{
    var activities = await app.WorkWith().Data<Activity>().Get().
   Where(activity => activity.CreatedAt > DateTime.Now.AddHours(-1)).
   ExecuteAsync();

    foreach(var activity in activities)    
    {
        Debug.WriteLine("Activity: " + activity.Id);
    }
}

You can also use the $lt, $gte and $lte operators to make correspondingly less-than, greater-than-or-equal-to, and less-than-or-equal-to comparisons.

Value in array of possible matches

You can also use the $nin operator, which selects objects for which the specified field does not have any value in the specified array.

Boolean expressions

You can combine multiple filter expressions to get items. For example, to get items, which meets a list of criteria, you have to execute the following code snippet:

public async Task GetRecentNotLikedActivities(EverliveApp app)
{
    string typeName = "Activities";
    var activities = await app.WorkWith().Data<Activity>().Get().
   Where(activity => activity.CreatedAt > DateTime.Now.AddHours(-1) && activity.Likes == null).
   ExecuteAsync();

    foreach(var activity in activities)    
    {
        Debug.WriteLine("Activity: " + activity.Id);

    }
}

You can also use the $nor and $or operators to get items which meet none or either of the conditions in the list.

Multiple criteria

The following example uses a compound filter to return all books by a given author that were published by a given publishing house.

public async Task GetAuthorBooksByPublisher(EverliveApp app)
{
    var books = await app.WorkWith().Data<Books>().Get().
      Where(book => book.Author == "Jaden R. Smith" && book.Publisher == "American Literature").
      ExecuteAsync();

   foreach(var book in books)
    {
        Debug.WriteLine("Book: " + book.Id);
    }
}

Deleting by filter

You can delete filtered items by executing the following code snippet:

public async Task DeleteActivitiesByUserId(EverliveApp app, string userId)
{
    await app.WorkWith().Data<Activity>().Delete().
   Where(activity => activity.UserId == userId).
   ExecuteAsync();
}

Update by filter

To update filtered items, you have to executing the following code snippet:

public async Task ChangeAuthorOfActivitiesName(EverliveApp app, string oldUserId, string newUserId)
{
    var updateObject = new Activity()
    {
        UserId = newUserId
    };
    await app.WorkWith().Data<Activity>().Update(updateObject).Where(activity => activity.UserId == userId).ExecuteAsync();
}

Examples for using Condition classes and String expression

Here is an example for using Condition classes:

public async Task GetRecentActivitiesByAuthors(EverliveApp app, string[] userIds, DateTime date)
{
    Condition containsCond = new ArrayCondition("UserId",ArrayConditionOperator.ValueIsIn,userIds);
    Condition valueCond = new ValueCondition("CreatedAt",ValueConditionOperator.ValueGreaterThanOrEqual,date);
    Condition condition = new CompoundCondition(CompoundConditionOperator.And, containsCond, valueCond);
    var activities = await app.WorkWith().Data<Activity>().Get().Where(condition).ExecuteAsync();
}

Here is an example for using String expression and $where syntax to filter items:

public async Task GetRecentActivitiesByAuthors(EverliveApp app, string jsFilterExpression)
{
    var activities = await app.WorkWith().Data<Activity>().Get().Where(jsFilterExpression).ExecuteAsync();
}
Start a free trial Request a demo
Contact us: +1-888-365-2779
sales@telerik.com
Copyright © 2016-2017, Progress Software Corporation and/or its subsidiaries or affiliates. All rights reserved.