Does LINQ work with IEnumerable?

前端 未结 3 1931
走了就别回头了
走了就别回头了 2020-11-30 01:04

I have a class that implements IEnumerable, but doesn\'t implement IEnumerable. I can\'t change this class, and I can\'t use another class

3条回答
  •  栀梦
    栀梦 (楼主)
    2020-11-30 01:43

    Yes it can. You just need to use the Cast function to get it converted to a typed IEnumerable. For example:

    IEnumerable e = ...;
    IEnumerable e2 = e.Cast();
    
    
    

    Now e2 is an IEnumerable and can work with all LINQ functions.

    提交回复
    热议问题