Axo*_*onn 634 c# linq ienumerable list
我对枚举器如何工作以及LINQ有些怀疑.考虑这两个简单的选择:
List<Animal> sel = (from animal in Animals
join race in Species
on animal.SpeciesKey equals race.SpeciesKey
select animal).Distinct().ToList();
Run Code Online (Sandbox Code Playgroud)
要么
IEnumerable<Animal> sel = (from animal in Animals
join race in Species
on animal.SpeciesKey equals race.SpeciesKey
select animal).Distinct();
Run Code Online (Sandbox Code Playgroud)
我更改了原始对象的名称,因此这看起来像一个更通用的示例.查询本身并不重要.我想问的是:
foreach (Animal animal in sel) { /*do stuff*/ }
Run Code Online (Sandbox Code Playgroud)
我注意到,如果我使用IEnumerable,当我调试并检查"sel"时,在这种情况下是IEnumerable,它有一些有趣的成员:"inner","outer","innerKeySelector"和"outerKeySelector",这些最后2个出现成为代表."内部"成员中没有"Animal"实例,而是"Species"实例,这对我来说非常奇怪."外部"成员确实包含"Animal"实例.我假设两位代表确定哪些进入,哪些进出?
我注意到如果我使用"Distinct","inner"包含6个项目(这是不正确的,因为只有2个是Distinct),但"outer"确实包含正确的值.同样,委托方法可能决定了这一点,但这比我对IEnumerable的了解要多一些.
最重要的是,两种选择中的哪一种是性能最佳的?
邪恶的名单转换通过.ToList()?
或者直接使用枚举器?
如果可以,请解释一下或抛出一些解释IEnumerable使用的链接.
Chr*_*ham 709
IEnumerable描述行为,而List是该行为的实现.当您使用时IEnumerable,您可以让编译器有机会将工作推迟到以后,可能会在此过程中进行优化.如果使用ToList(),则强制编译器立即重新生成结果.
每当我"堆叠"LINQ表达式时,我都会使用IEnumerable,因为通过仅指定行为,我给LINQ提供了推迟评估并可能优化程序的机会.还记得LINQ如何在枚举之前不生成用于查询数据库的SQL吗?考虑一下:
public IEnumerable<Animals> AllSpotted()
{
return from a in Zoo.Animals
where a.coat.HasSpots == true
select a;
}
public IEnumerable<Animals> Feline(IEnumerable<Animals> sample)
{
return from a in sample
where a.race.Family == "Felidae"
select a;
}
public IEnumerable<Animals> Canine(IEnumerable<Animals> sample)
{
return from a in sample
where a.race.Family == "Canidae"
select a;
}
Run Code Online (Sandbox Code Playgroud)
现在你有一个方法可以选择一个初始样本("AllSpotted"),还有一些过滤器.所以现在你可以这样做:
var Leopards = Feline(AllSpotted());
var Hyenas = Canine(AllSpotted());
Run Code Online (Sandbox Code Playgroud)
那么使用List会更快IEnumerable吗?仅当您要阻止查询多次执行时.但总体来说它更好吗?在上面,Leopards和Hyenas 各自转换为单个SQL查询,数据库只返回相关的行.但是如果我们从中返回了List AllSpotted(),那么它可能会运行得更慢,因为数据库可能返回的数据远远超过实际需要的数据,并且我们浪费了在客户端进行过滤的循环.
在一个程序中,最好将查询推迟到列表直到最后,所以如果我要通过Leopards和Hyenas多次枚举,我会这样做:
List<Animals> Leopards = Feline(AllSpotted()).ToList();
List<Animals> Hyenas = Canine(AllSpotted()).ToList();
Run Code Online (Sandbox Code Playgroud)
rub*_*low 150
有一篇非常好的文章写道:Claudio Bernasconi的TechBlog在这里:何时使用IEnumerable,ICollection,IList和List
这里有一些关于场景和功能的基础知识:

Kei*_*ith 128
实现的类IEnumerable允许您使用foreach语法.
基本上它有一个方法来获取集合中的下一个项目.它不需要整个集合在内存中并且不知道其中有多少项,foreach只是不断获取下一个项目直到它用完为止.
这在某些情况下非常有用,例如在大型数据库表中,您不希望在开始处理行之前将整个事物复制到内存中.
现在List实现IEnumerable,但代表内存中的整个集合.如果你有一个IEnumerable并且你打电话给.ToList()你创建一个新列表,其中包含内存中枚举的内容.
你的linq表达式返回一个枚举,默认情况下,当你迭代使用时,表达式会执行foreach.一个IEnumerableLINQ语句时,你遍历执行foreach,但可以迫使它更快地迭代使用.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 ) ...
Run Code Online (Sandbox Code Playgroud)
the*_*oop 66
最重要的事情是,使用Linq,查询不会立即得到评估.它只能运行通过所产生的迭代的一部分IEnumerable<T>的foreach-这就是所有的怪异的代表都在做.
因此,第一个示例通过调用ToList并将查询结果放入列表中立即评估查询.
第二个示例返回一个IEnumerable<T>包含稍后运行查询所需的所有信息的示例.
在性能方面,答案取决于它.如果您需要立即评估结果(例如,您正在改变稍后要查询的结构,或者如果您不希望迭代结果IEnumerable<T>需要花费很长时间),请使用列表.否则使用IEnumerable<T>.默认情况下应该是在第二个示例中使用按需评估,因为通常使用较少的内存,除非有特定的原因将结果存储在列表中.
Mat*_*man 39
IEnumerable的优点是延迟执行(通常使用数据库).在实际循环数据之前,查询将不会执行.这是一个等待它需要的查询(也就是延迟加载).
如果你调用ToList,那么查询将被执行,或者就像我想说的那样"实现".
这两者都有利有弊.如果你调用ToList,你可能会删除一些关于何时执行查询的谜团.如果你坚持IEnumerable,你会得到这样的好处,即程序在实际需要之前不会做任何工作.
amd*_*amd 22
我将分享一下我有一天误入歧途的概念:
var names = new List<string> {"mercedes", "mazda", "bmw", "fiat", "ferrari"};
var startingWith_M = names.Where(x => x.StartsWith("m"));
var startingWith_F = names.Where(x => x.StartsWith("f"));
// updating existing list
names[0] = "ford";
// Guess what should be printed before continuing
print( startingWith_M.ToList() );
print( startingWith_F.ToList() );
Run Code Online (Sandbox Code Playgroud)
// I was expecting
print( startingWith_M.ToList() ); // mercedes, mazda
print( startingWith_F.ToList() ); // fiat, ferrari
Run Code Online (Sandbox Code Playgroud)
// what printed actualy
print( startingWith_M.ToList() ); // mazda
print( startingWith_F.ToList() ); // ford, fiat, ferrari
Run Code Online (Sandbox Code Playgroud)
根据其他答案,结果的评估被推迟到调用ToList或类似的调用方法为例ToArray.
所以我可以在这种情况下重写代码:
var names = new List<string> {"mercedes", "mazda", "bmw", "fiat", "ferrari"};
// updating existing list
names[0] = "ford";
// before calling ToList directly
var startingWith_M = names.Where(x => x.StartsWith("m"));
var startingWith_F = names.Where(x => x.StartsWith("f"));
print( startingWith_M.ToList() );
print( startingWith_F.ToList() );
Run Code Online (Sandbox Code Playgroud)
Dar*_*mas 15
如果您只想枚举它们,请使用IEnumerable.
但要注意,更改要枚举的原始集合是一项危险的操作 - 在这种情况下,您需要ToList先进行操作.这将为内存中的每个元素创建一个新的列表元素,IEnumerable如果您只枚举一次,则枚举并且因此性能较差 - 但更安全,有时List方法很方便(例如在随机访问中).
小智 8
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;
Run Code Online (Sandbox Code Playgroud)
这是行不通的
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.
Run Code Online (Sandbox Code Playgroud)
除了上面发布的所有答案,这是我的两分钱.除了List之外还有许多其他类型实现IEnumerable,如ICollection,ArrayList等.因此,如果我们将IEnumerable作为任何方法的参数,我们可以将任何集合类型传递给函数.即我们可以使用方法来操作抽象而不是任何特定的实现.
| 归档时间: |
|
| 查看次数: |
423671 次 |
| 最近记录: |