Generally speaking, LINQ queries are executed when the application code processes
data (for instance, using a foreach or a for) or when certain methods are invoked
(ToList, ToArray, First, Single, and so on).

 下面的方法将会查询两次数据库:

var result = LINQToEntitiesQuery;
foreach(var o in result)
{
...
}

foreach(var o in result)
{
...
}

为了避免这种情况引起的性能问题,这种情况下需要将数据提前ToList(),代码如下:

var orders = LINQToEntitiesQuery.ToList();
foreach(var o in orders)
{
...
}
foreach(var o in orders)
{
...
}

 

相关文章:

  • 2022-12-23
  • 2022-12-23
  • 2021-08-29
  • 2021-12-27
  • 2021-08-08
  • 2021-07-13
  • 2022-02-04
  • 2022-12-23
猜你喜欢
  • 2021-06-23
  • 2021-10-15
  • 2021-11-11
  • 2021-11-04
  • 2021-12-23
  • 2022-12-23
相关资源
相似解决方案