๐ก 7 Tips to write better LINQ Queries
๐๐ฌ๐ ๐จ๐ ๐๐ฌ๐๐จ๐๐ซ๐๐๐ค๐ข๐ง๐ : For read only queries e.g. (GetAll,GetById etc.) use AsNoTracking , when we use it entities are not tracked for change so it brings data more speedily. #dotnet
See thread ๐งต๐ฝ
๐๐ง๐๐ฅ๐ฎ๐๐ ๐ง๐๐๐๐ฌ๐ฌ๐๐ซ๐ฒ ๐๐ง๐ญ๐ข๐ญ๐ข๐๐ฌ ๐๐ง๐ ๐๐จ๐ฅ๐ฎ๐ฆ๐ง๐ฌ : While retrieving data from multiple table make sure to include only necessary tables and columns Use eager loading only when it is necessary.
#dotnet
๐
๐จ๐ซ ๐ฅ๐๐ซ๐ ๐ ๐๐๐ญ๐ ๐ฎ๐ฌ๐ ๐๐ค๐ข๐ฉ ๐๐ง๐ ๐๐๐ค๐ : Use skip and take to retrieve data from table for large collections because if we try to bring all data in single try it can take time that will give bad user experience takes next values.
#dotnet
๐๐ฌ๐ ๐๐ฌ๐ฒ๐ง๐ ๐ฆ๐๐ญ๐ก๐จ๐๐ฌ : For better user experience use async methods e.g. FirstOrDefaultAsync , SingleOrDefaultAsync and ToListAsync.
#dotnet
๐๐๐๐ฎ๐๐ ๐๐ซ๐ข๐ฉ๐ฌ ๐ญ๐จ ๐๐๐ญ๐๐๐๐ฌ๐ : Use bulk operations available for SAVE/DELETE operations instead of iterating one by one it will reduce trips to database. After that only write one line await _context.SaveChangesAsync(); to reflect changes over database.
#dotnet
๐๐ฌ๐ ๐๐ซ๐ฒ๐๐๐ญ๐๐จ๐ง๐๐ง๐ฎ๐ฆ๐๐ซ๐๐ญ๐๐๐๐จ๐ฎ๐ง๐ญ : For large of data we use pagination and it is displayed on basis of total records. Use TryGetNonEnumeratedCount instead of count it attempts to determine the number of elements in a sequence without forcing an enumeration
๐๐ฌ๐ ๐๐๐ฎ๐๐ซ๐ฒ๐๐๐ฅ๐ : For long and condition based filters use IQueryable,it executes queries on the server side.After all conditions we can use ToList to fetch data it makes query faster as we are first creating complete query and then we are bringing the data
#dotnet
๐ง If you like it join family of 550+ Software Engineers via my Newsletter : lnkd.in/dNHxJGRG
#dotnet
Share this Scrolly Tale with your friends.
A Scrolly Tale is a new way to read Twitter threads with a more visually immersive experience.
Discover more beautiful Scrolly Tales like this.