简体   繁体   中英

How can I convert IOrderedEnumerable<T> & IEnumerable<IGrouping<T>> & IEnumerable<T> to one common format?

I have 3 "Lists" all of different types and at the moment I have to pass them to 3 different functions to go output their contents because I don't know how to convert them to one type.

IEnumerable<IGrouping<string, DynamicNode>>
IOrderedEnumerable<DynamicNode>
IEnumerable<DynamicNode>

How can I create a function to accept all 3 types so I can loop through them there without creating 3 different functions and duplicating code?

public void constructHtml(???? nodeList){
    foreach(dynamic child in nodeList){
        //etc..
    }
}

You could always create a generic method and check the type in the method. Something like

public void constructHtml<T>(T nodeList){
    // Check type T and do something with it
}

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM