LINQ - Filtering Operators
Above query returns only "Sheraz" and narrows down the search to strings only.
The two filtering operators in LINQ are the Where and OfType operators. An example follows.
ArrayList list = new ArrayList { "Sheraz", new object(), "London", new object() };
var query = from name in list.OfType<string>()
where name == "Sheraz"
select name;
Above query returns only "Sheraz" and narrows down the search to strings only.
The Where operator generally translates to a WHERE clause when working with a relational database and has an associated keyword in C# as shown in the code above. The OfType operator can be used to coax a non-generic collection (like an ArrayList) into a LINQ query. Since ArrayList does not implement IEnumerable , the OfType operator is the only LINQ operator we can apply to the list.OfType is also useful if you are working with an inheritance hierarchy and only want to select objects of a specific subtype from a collection. This includes scenarios where LINQ to SQL or the Entity Framework are used to model inheritance in the database. Both operators are deferred.
No comments:
Post a Comment