LINQ operators are a collection of methods that form the heart and soul of the LINQ(Language
Integrated Query). The best thing of these operators is how they can execute
against different data sources. You can use these operators not only to query object
in memory but you can use same operators to query object stored in relational
database, in XML, datasets and more.
Standard LINQ Operators:
We can
categories the LINQ operators into following area of functionality
- Filtering
- Projecting
- Ordering
- Grouping
- Conversions
- Joining
- Aggregation
- Elements
- Sets
Filtering Operators:
Where and OfType
are two filtering operators in LINQ .
- Where: The
Where
can used to filter a set of items. This is also called restriction operator because it restricts a set of items
//array of numbers
int[] numbers={2,4,5,6,7,9,10};
//select numbers which are greater than 2var number = from n in numberswhere n > 2 && n < 7select n; /*output 4, 5, 6*/ - OfType:
The OfType
can used to filter values based on the type (can use on IEnumerable). For example if we have an array
of objects which contains strings, decimal, float and numbers or any other. We
can use OfType operator to extract only strings or any particular type of
values.
ArrayList list = new ArrayList();list.Add("Dash");list.Add(new object());
list.Add("Skitty");
list.Add(new object());
var query = from name in list.OfType<string>()select name; /* output Dash, Skitty*/
To be continued......
Happy Reading!!
No comments:
Post a Comment