Filtering refers to the operations of restricting the result set to contain only those elements that satisfy a specified condition.
Where
Selects values that are based on a predicate function.
Method Syntax
// Where
var numbers = new int[] { 1, 2, 3, 4, 5, 6, 7, 8 };
var evens = numbers.Where(n => n % 2 == 0);
// evens = { 2, 4, 6, 8 }
Query Syntax
// where
var numbers = new int[] { 1, 2, 3, 4, 5, 6, 7, 8 };
var odds = from n in numbers
where n % 2 != 0
select n;
// odds = { 1, 3, 5, 7 }
OfType
Selects values, depending on their ability to be cast to a specified type.
Method Syntax
// OfType
var numbers = new object[] { 1, "one", 2, "two", 3, "three" };
var strings = numbers.OfType<string>();
// strings = { "one", "two", "three" }
Query Syntax
// Not applicable.