Programming Guide > Using ClientView in Code > Server-Side Filtering Views |
To apply filtering to a client view, use the AsFiltered method method returning FilteredView, a class derived from ClientView:
FilteredView<Product> productsByCategory = products.AsFiltered(p => p.CategoryID == categoryID);
Here categoryID is a variable or parameter that is assigned a value somewhere else in the code. When you create this view, with a certain value of categoryID, say 1, it will retrieve products with p.CategoryID = 1. It will do it using the cache, without a trip to the server, if possible; if not, it will fetch those entities from the server (that's the cache-awareness feature of client views). Unlike a standard EF (or RIA) query, it will also take into account entities that are added on the client and don't yet exist on the server, and entities modified on the client so they now satisfy the filter condition (that's the live feature of client views).
Unlike a standard EF (or RIA) query, this view will remain up to date (live) after it was first used. If you modify data on the client and you now have more (or less) entities satisfying the condition, the view will be automatically updated to reflect the changed data. And if you have GUI controls bound to it, those will be updated too.
If you use the AsFiltered method with key selector function
FilteredView<Product> productsByCategory = products.AsFilteredBound(p => p.CategoryID);
you will be able to set/change the filter condition without creating a new view every time:
productsByCategory.FilterKey = categoryID;
There is also a convenience method, BindFilterKey, you can use to bind the filter key of a view to a property (or property path) of an object:
FilteredView<Product> productsByCategory = products.AsFilteredBound(p => p.CategoryID)
.BindFilterKey(comboBox1, "SelectedValue")