Slow foreach() on a LINQ query - ToList() boosts performance immensely - why is this?

前端 未结 3 1812
猫巷女王i
猫巷女王i 2021-01-04 07:59

I kind of grasp the whole delayed execution concept, but the following has me puzzled...

On a DataTable containing about 1000 rows, I call AsEnumerable(). I

3条回答
  •  野趣味
    野趣味 (楼主)
    2021-01-04 08:12

    Indeed you seem to have no clear idea what is execution of code and what is definition of the intent to (possibly) execute later when the results are actually used. I suggest reading up one this part of LINQ.

    And possibly try executing both of your variants with a debugger attached so you can actually see what code is executing in which order and what is actually happening to your data. You might be in for a (big?) surprise here...

提交回复
热议问题