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

前端 未结 3 1836
猫巷女王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:25

    It will not get all items from database until you type

     ToList or First or Single
    

    In foreach, you send a query into database for each item. So it works slower. Open your sql profiler to understand better.

提交回复
热议问题