๐“๐ข๐ฉ๐ฌ ๐ญ๐จ ๐ฐ๐ซ๐ข๐ญ๐ž ๐›๐ž๐ญ๐ญ๐ž๐ซ ๐‹๐ˆ๐๐ ๐๐ฎ๐ž๐ซ๐ข๐ž๐ฌ

There are few tips that can help you in writing 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.

๐ˆ๐ง๐œ๐ฅ๐ฎ๐๐ž ๐ง๐ž๐œ๐ž๐ฌ๐ฌ๐š๐ซ๐ฒ ๐„๐ง๐ญ๐ข๐ญ๐ข๐ž๐ฌ ๐š๐ง๐ ๐‚๐จ๐ฅ๐ฎ๐ฆ๐ง๐ฌ : While retrieving data from multiple table make sure to include only necessary tables if you add tables that are not needed it will increase headache of query. Use eager loading only when it is necessary. Similarly donโ€™t select the all columns from table , just retrieve necessary columns information using SELECT

๐…๐จ๐ซ ๐ฅ๐š๐ซ๐ ๐ž ๐๐š๐ญ๐š ๐ฎ๐ฌ๐ž ๐’๐ค๐ข๐ฉ ๐š๐ง๐ ๐“๐š๐ค๐ž : When you are dealing with a lot of information use skip and Take to retrieve data from table because if we try to bring all data in single try it can take time that will give bad user experience Skip & Take two integer and skips provided values and then takes next values.

๐”๐ฌ๐ž ๐š๐ฌ๐ฒ๐ง๐œ ๐ฆ๐ž๐ญ๐ก๐จ๐๐ฌ : For better user experience use async methods e.g. FirstOrDefaultAsync , SingleOrDefaultAsync and ToListAsync

๐‘๐ž๐๐ฎ๐œ๐ž ๐“๐ซ๐ข๐ฉ๐ฌ ๐ญ๐จ ๐ƒ๐š๐ญ๐š๐›๐š๐ฌ๐ž : 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.

๐”๐ฌ๐ž ๐“๐ซ๐ฒ๐†๐ž๐ญ๐๐จ๐ง๐„๐ง๐ฎ๐ฆ๐ž๐ซ๐š๐ญ๐ž๐๐‚๐จ๐ฎ๐ง๐ญ : While working with large collection of data we use pagination and our pagination is displayed on the basis of total records available. So instead of .Count() method use TryGetNonEnumeratedCount it attempts to determine the number of elements in a sequence without forcing an enumeration.

๐”๐ฌ๐ž ๐จ๐Ÿ ๐ˆ๐๐ฎ๐ž๐ซ๐ฒ๐š๐›๐ฅ๐ž : When you are dealing with a long list of filters and your filters are condition based make use of IQueryable ,it executes queries on the server side. After all conditions we can useย .ToList()ย to fetch data , so it makes query faster because we are first creating complete query and then we are bringing the data.

There are few tips that can help you in writing better LINQ Queries.

Related posts

Artificial Intelligence Engineer
Artificial Intelligence Engineer

View More
Tips For A Standout IT Resume
Tips For A Standout IT Resume

IT is a competitive field, and that can make harder to stand out from the crowd. Your IT resumรฉ can help you do that โ€” if you do it right

View More
Helpful extensions for flutter developers
Helpful extensions for flutter developers

View More

Leave a Reply

Your email address will not be published.