Coproject - a RIA Caliburn.Micro demo, part 7

by Augustin Šulc

In this part, we will add filtering to ToDo list and create several useful extension methods.

Extension methods

In the client project root, create a new static class called Extensions and put following functions into it:

public static bool IsNullOrWhiteSpace(this string value)
	return string.IsNullOrWhiteSpace(value);

public static string FormatWith(this string formatString, params object[] args)
	return string.Format(formatString, args);

public static IResult ToSequential(this IEnumerable<IResult> results)
	return new SequentialResult(results.GetEnumerator());

The last function is interesting. What it does is that it wraps a collection of IResults (remember coroutines from the last part?) into one result. This is particularly useful when you want to yield return a collection of results, maybe other coroutine function.


To add filtering capabilities to our ToDoListView, we must add a textbox to it. In order to make look Coproject look nice, add some more styling, so replace LoadData button from the view with the following:

<Border Style="{StaticResource FilterPanelStyle}">
			<ColumnDefinition Width="*" />
			<ColumnDefinition Width="auto" />

		<TextBox x:Name="Filter" Style="{StaticResource FilterTextBoxStyle}" />
		<Button x:Name="LoadData" Grid.Column="1" Content="Search" />

In fact, we just added a TextBox called Filter. Now, we have to make use of it. Update LoadData function in ToDoListsViewModel:

public IEnumerable<IResult> LoadData(string filter)
	CoprojectContext context = new CoprojectContext();
	EntityQuery<ToDoList> query = context.GetToDoListsWithItemsQuery().OrderByDescending(l => l.CreatedDate);

	if (!filter.IsNullOrWhiteSpace())
		query = query.Where(x => x.Name.Contains(filter));

	var result = new LoadDataResult<ToDoList>(context, query);
	yield return result;

	Lists = result.Result.Entities;
	NotifyOfPropertyChange(() => Lists);

As you can see, we only added parameter and filter query according to the parameter. Btw: this filter is transferred via RIA services and Entity Framework to database server and executed there (so the SQL statement is where Name like ‘%anyText%’). Using this way of filtering, you can easily chain multiple filter criteria. You probably won’t be surprised that the filter parameter is set by Caliburn.Micro according to the content of the TextBox called Filter.

Depending on database configuration, the comparison might be case sensitive or in-sensitive. If it is case sensitive, you will need to change the criteria to:

query = query.Where(x => x.Name.ToLower().Contains(filter.ToLower()));

That is all for this part about filtering.

Tags: Silverlight, Caliburn, Ria, Coproject

Add a Comment