我有一个返回 List<SampleClass>
的 Web 服务.我使用 LINQ-to-SQL 从数据库中获取数据作为 IEnumerable<SampleClass>.
然后我转换 IEnumerable<SampleClass>
至 List<SampleClass>
.
LINQ-to-SQL 操作性能还可以,但是 IEnumerable<SampleClass>
至 List<SampleClass>
需要一些时间来做手术。是否有任何解决方案可以从 IEnumerable<SampleClass>
获得最佳性能至 List<SampleClass>
?
我从我的数据库中读取了 3000 多条记录。
谢谢
最佳答案
IEnumerable to List takes some time to do the operation.
出现延迟的原因是,当您执行 ToList
时,即执行实际查询并从数据库中获取记录的时间。
这叫做 Deferred execution .
var query = db.yourTable.Where(r=> r.ID > 10);
var List = query.ToList(); //this is where the actual query gets executed.
当您使用 ToList
、ToArray
、Count()
等遍历查询时,即实际查询被执行的时候。
Are there any solutions to get best performance from IEnumerable to List? I read more than 3000 records from my database.
关于c# - 将 IEnumerable<T> 转换为 List<T> 性能,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/13509487/