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

前端 未结 5 1037
青春惊慌失措
青春惊慌失措 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:20

    From LINQPad:

    C#:

    age_Centers.Select(c => c.Id).First();
    age_Centers.Select(c => c.Id).FirstOrDefault();
    age_Centers.Select(c => c.Id).Take(1).Dump();
    

    SQL:

    SELECT TOP (1) [t0].[Id]
    FROM [age_Centers] AS [t0]
    GO
    
    SELECT TOP (1) [t0].[Id]
    FROM [age_Centers] AS [t0]
    GO
    
    SELECT TOP (1) [t0].[Id]
    FROM [age_Centers] AS [t0]
    

    *Note that Take(1) enumerates and returns an IQueryable.

提交回复
热议问题