When using Linq to build query, developers often face the choice of and
as a problem of target type. To clarify this choice, let us explore their behavior and performance differences.
IEnumerable
Functional vs. Similar List
It represents an abstract concept and describes how to iterate data. It does not have a specified data storage or access method. On the other hand, is the specific implementation of , which stores data in a fixed -size array. When using , the compiler can be more flexibly delayed and optimized.
Example: Linq query and Ienumeration IEnumerable
List
Consider the following linq query: IEnumerable
IEnumerable
operational symbols to return . In , internal members such as "internal" and "external" have internal members to manage inquiry and evaluation.
Delayed evaluation vs. Eager evaluation
<code class="language-csharp">IEnumerable<animal> sel = (from animal in Animals join race in Species on animal.SpeciesKey equals race.SpeciesKey select animal).Distinct();</code>
object. Distinct()
IEnumerable<animal>
When to use Ienumeration IEnumerable
When the following situations occur, More:
You intend to stack multiple Linq expressions to allow delayed evaluation and optimization. IEnumerable
ToList()
You want to avoid unnecessary processing by returning only data that needs only actual needs. List
When to use list
When the following situations occur, is more favorable: IEnumerable
In terms of performance, if the query executes multiple or for remote data source execution,
may be more efficient than. However, if you need to repeatedly access the query results, List
can provide better performance by avoiding multiple evaluations.
The above is the detailed content of IEnumerable vs. List in LINQ: When Should You Use Each?. For more information, please follow other related articles on the PHP Chinese website!