Wednesday 16 December 2015

ASP.NET Tips #61 - Confirm you are retrieving only the records you need

If you are calling the database using Entity Framework, you may write this command:

return context.Products.ToList().FirstOrDefault();

While this essentially returns one record, it retrieves the entire Products table from the database and then returns you one record. A better command would be:

return context.Products.FirstOrDefault();

This will produce a SQL command equivalent to grabbing the TOP 1 record instead of the entire contents of the table.

No comments :

Post a Comment