IEnumerable vs List - What to Use? How do they work?

后端 未结 10 2419
臣服心动
臣服心动 2020-11-22 03:57

I have some doubts over how Enumerators work, and LINQ. Consider these two simple selects:

List sel = (from animal in Animals 
                         


        
10条回答
  •  刺人心
    刺人心 (楼主)
    2020-11-22 04:33

    IEnumerable describes behavior, while List is an implementation of that behavior. When you use IEnumerable, you give the compiler a chance to defer work until later, possibly optimizing along the way. If you use ToList() you force the compiler to reify the results right away.

    Whenever I'm "stacking" LINQ expressions, I use IEnumerable, because by only specifying the behavior I give LINQ a chance to defer evaluation and possibly optimize the program. Remember how LINQ doesn't generate the SQL to query the database until you enumerate it? Consider this:

    public IEnumerable AllSpotted()
    {
        return from a in Zoo.Animals
               where a.coat.HasSpots == true
               select a;
    }
    
    public IEnumerable Feline(IEnumerable sample)
    {
        return from a in sample
               where a.race.Family == "Felidae"
               select a;
    }
    
    public IEnumerable Canine(IEnumerable sample)
    {
        return from a in sample
               where a.race.Family == "Canidae"
               select a;
    }
    

    Now you have a method that selects an initial sample ("AllSpotted"), plus some filters. So now you can do this:

    var Leopards = Feline(AllSpotted());
    var Hyenas = Canine(AllSpotted());
    

    So is it faster to use List over IEnumerable? Only if you want to prevent a query from being executed more than once. But is it better overall? Well in the above, Leopards and Hyenas get converted into single SQL queries each, and the database only returns the rows that are relevant. But if we had returned a List from AllSpotted(), then it may run slower because the database could return far more data than is actually needed, and we waste cycles doing the filtering in the client.

    In a program, it may be better to defer converting your query to a list until the very end, so if I'm going to enumerate through Leopards and Hyenas more than once, I'd do this:

    List Leopards = Feline(AllSpotted()).ToList();
    List Hyenas = Canine(AllSpotted()).ToList();
    

提交回复
热议问题