With Entity Framework is it better to use .First() or .Take(1) for “TOP 1”?

前端 未结 5 1039
青春惊慌失措
青春惊慌失措 2021-01-01 08:55

We are implementing some EF data repositories, and we have some queries which would include TOP 1

I have read many posts suggesting to use .Ta

5条回答
  •  迷失自我
    2021-01-01 09:14

    First will query Take 1, so there is no difference in query. Calling FirstOrDefault will be one step statement, because Take returns IEnumerable do you will need to call First anyway.

    First will throw exception so FirstOrDefault is always preferred.

    And ofcourse people who wrote EF query converter are smart enough to call Take 1 instead executing entire result set and returning first item.

    You can this verify using SQL profiler.

提交回复
热议问题