我对枚举器和LINQ的工作方式有些怀疑。考虑以下两个简单的选择:

List<Animal> sel = (from animal in Animals 
                    join race in Species
                    on animal.SpeciesKey equals race.SpeciesKey
                    select animal).Distinct().ToList();

or

IEnumerable<Animal> sel = (from animal in Animals 
                           join race in Species
                           on animal.SpeciesKey equals race.SpeciesKey
                           select animal).Distinct();

我更改了原始对象的名称,使其看起来像一个更通用的示例。查询本身并不那么重要。我想问的是:

foreach (Animal animal in sel) { /*do stuff*/ }

我注意到,如果我使用IEnumerable,当我调试和检查“sel”(在这种情况下是IEnumeraable)时,它有一些有趣的成员:“internal”、“outer”、“innerKeySelector”和“outerKeySelecter”,最后两个似乎是委托。“内部”成员中没有“动物”实例,而是“物种”实例,这对我来说很奇怪。我想这两位代表会决定哪一个进入,哪一个退出?我注意到,如果我使用“Distinct”,“inner”包含6项(这是不正确的,因为只有2项是Distinct),但“outer”包含正确的值。同样,可能是委托方法决定了这一点,但这比我对IEnumerable的了解要多得多。最重要的是,这两个选项中哪一个性能最佳?

通过.ToList()进行的邪恶列表转换?

或者直接使用枚举器?

如果可以的话,也请解释一下或提供一些链接来解释IEnumerable的用法。


当前回答

IEnumerable(延迟执行)的缺点是,在调用.ToList()之前,列表可能会发生更改。举一个非常简单的例子,这是可行的

var persons;
using (MyEntities db = new MyEntities()) {
    persons = db.Persons.ToList(); // It's mine now. In the memory
}
// do what you want with the list of persons;

这是行不通的

IEnumerable<Person> persons;
 using (MyEntities db = new MyEntities()) {
     persons = db.Persons; // nothing is brought until you use it;
 }

persons = persons.ToList();  // trying to use it...
// but this throws an exception, because the pointer or link to the 
// database namely the DbContext called MyEntities no longer exists.

其他回答

IEnumerable(延迟执行)的缺点是,在调用.ToList()之前,列表可能会发生更改。举一个非常简单的例子,这是可行的

var persons;
using (MyEntities db = new MyEntities()) {
    persons = db.Persons.ToList(); // It's mine now. In the memory
}
// do what you want with the list of persons;

这是行不通的

IEnumerable<Person> persons;
 using (MyEntities db = new MyEntities()) {
     persons = db.Persons; // nothing is brought until you use it;
 }

persons = persons.ToList();  // trying to use it...
// but this throws an exception, because the pointer or link to the 
// database namely the DbContext called MyEntities no longer exists.

如果您只想枚举它们,请使用IEnumerable。

不过,请注意,更改正在枚举的原始集合是一个危险的操作——在这种情况下,您将需要首先ToList。这将为内存中的每个元素创建一个新的列表元素,枚举IEnumerable,因此如果只枚举一次,性能会降低-但更安全,有时list方法很方便(例如在随机访问中)。

这里有一篇很好的文章:Claudio Bernasconi的TechBlog:何时使用IEnumerable、ICollection、IList和List

以下是一些关于场景和功能的基本要点:

在许多情况下(例如无限列表或非常大的列表),IEnumerable无法转换为list。最明显的例子是所有的质数,facebook的所有用户及其详细信息,或者ebay上的所有项目。

不同之处在于,“List”对象“就在此时此地”存储,而“IEnumerable”对象“一次只能存储一个”。所以,如果我在ebay上浏览所有项目,一次一个,即使是一台小型计算机也能处理,但“.ToList()”肯定会耗尽我的内存,无论我的计算机有多大。没有一台计算机能够单独包含和处理如此庞大的数据量。

[编辑]-不用说-这不是“这个或那个”。通常,在同一类中同时使用列表和IEnumerable是很有意义的。世界上没有一台计算机能列出所有素数,因为根据定义,这需要无限的内存。但是您可以很容易地想到一个类PrimeContainer,它包含IEnumerable<long>素数,由于明显的原因,它还包含SortedList<long<_primes。迄今为止计算的所有素数。要检查的下一个素数将只针对现有素数运行(直到平方根)。通过这种方式,您可以同时获得一个素数(IEnumerable)和一个很好的“迄今为止的素数”列表,这是整个(无限)列表的很好的近似值。

实现IEnumerable的类允许您使用foreach语法。

基本上,它有一个获取集合中下一个项的方法。它不需要将整个集合存储在内存中,也不知道其中有多少项,foreach只需要不断获取下一项,直到用完为止。

这在某些情况下非常有用,例如在大型数据库表中,您不希望在开始处理行之前将整个内容复制到内存中。

现在List实现IEnumerable,但表示内存中的整个集合。如果您有一个IEnumerable,并调用.ToList(),则会在内存中创建一个包含枚举内容的新列表。

linq表达式返回一个枚举,默认情况下,当您使用foreach遍历时,该表达式将执行。迭代foreach时执行IEnumerable linq语句,但可以使用.ToList()强制其更快地迭代。

我的意思是:

var things = 
    from item in BigDatabaseCall()
    where ....
    select item;

// this will iterate through the entire linq statement:
int count = things.Count();

// this will stop after iterating the first one, but will execute the linq again
bool hasAnyRecs = things.Any();

// this will execute the linq statement *again*
foreach( var thing in things ) ...

// this will copy the results to a list in memory
var list = things.ToList()

// this won't iterate through again, the list knows how many items are in it
int count2 = list.Count();

// this won't execute the linq statement - we have it copied to the list
foreach( var thing in list ) ...