.net - Requirements for collection class to be used with LINQ -
i thought enough requirement class should satisfy able use where()
implement ienumerable
.
but today friend of mine asked me question, why cannot apply where()
object of spusercollection class (it sharepoint). since class derived spbasecollection implements ienumerable
- expected should fine. not.
any ideas, why?
the linq extension methods defined on ienumerable<t>
, not ienumerable
. example, see where<t>
signature:
public static ienumerable<tsource> where<tsource>( ienumerable<tsource> source, func<tsource, bool> predicate )
to mitigate issue, linq cast<t>
extension method turns ienumerable
ienumerable<t>
can used normal linq functions.
in example below, can't e.where(...)
, can cast
, use where
.
int[] xs = new[] { 1, 2, 3, 4 }; ienumerable e = xs; var odds = e.cast<int>().where(x => x % 2 == 1);
unfortunately, needs used lot when dealing pre-generics apis in .net bcl.
Comments
Post a Comment