LINQ Adventure 2 - Standard Query Operators - OfType Operator
Now I am moving towards the Standard Query Operators in LINQ programming. Things are getting
more interesting this time.
Question: What is OfType Operator?
Answer: OfType operator enables you to restrict or focus on a specific result set, say you only
want all the possible integers which are available in a particular array of object.
Example:
object[] arrValues = {1, 2, 8, 1.1, 5.9001, "chua", 'z', 'r', true, 4, false, "ching"};// Defines a list of possible formats to be displayed at end results
// expecting strings only
var strResults = arrValues.OfType<string>();
// expecting integer only
var intResults = arrValues.OfType<int>();
// expecting double only
var doubleResults = arrValues.OfType<double>();
// expecting characters only
var charResults = arrValues.OfType<char>();
// expecting boolean values
var boolResults = arrValues.OfType<bool>();
// display the bool values
foreach (var result in boolResults)
{Console.WriteLine(result);
}
Cheers.