We pay for user submitted tutorials and articles that we publish. Anyone can send in a contributionLearn More
I was into filtering items lately and I have decided to write about some of the techniques out there starting with the old
Well we have all used this before once you get a list in your hands just iterate through all the items and select the ones you want:
I hope you are not using this method. As there are other good methods like this one:
This method is much more simple and nice. first we will have to define a predicate that will point to a function that will hold the logic of the filtering. Here is the function that will provide the testing of whether the integer is positive or not:
Here is how we use it to filter all the integers:
That takes care of filtering using predicates. Not a bad way of doing it but here comes the slickest way of filtering:
This is just an improvement on the previous method instead of using the predicate we will provide List.FindAll with a lambda expression.
We will use the following expression:
n => n>0 which can be translated like this: n where n is larger then 0.
this lambda expression has a return type of boolean so it can be placed instead of the predicate:
Copyright © 2012 Dev102.com
Breeze : Designed by Amit Raz and Nitzan Kupererd