Jammer
Jammer

Reputation: 10206

Store Filter in Custom Collection and Conditionally Execute

I have Cusom collection object containing a collection of objects. I then have two UI elements (a grid and chart) bound to this single object. The grid shows all the objects and the chart shows a subset of the same collection.

What I want to be able to do is register a filter in the collection (Func<>?) and conditionally execute the filter to create the subset collection.

Really not sure how to approach this. Any pointers would be really helpful.

Upvotes: 1

Views: 206

Answers (2)

Teun D
Teun D

Reputation: 5099

You can just pass in the Func<> into the Where() static method. The output will be a new IEnumerable collection you can bind to your UI elements. The function you register must be of type Func<ItemInYourCollection, Boolean>.

If you want to switch between using the filter and not using the filter, it may be easiest to use a "non-filtering" function:

Func<ItemInYourCollection, Boolean> filter = (a) => true;
if(filtering) filter = registeredFilter;
UI.Data = rawData.Where(filter);

Upvotes: 1

MarcinJuraszek
MarcinJuraszek

Reputation: 125650

You can use LINQ Where extension method or syntax-based query to indicate which elements should be placed in that chart. It would be possible only if your collection implements IEnumarable interface.

An example of Where extension method could be something like that:

var filteredItem = items.Where(i => i.type == typeYouLookFor)

It uses lambda expression so you don't have to declare any additional function.

Upvotes: 0

Related Questions