7 Tips to write better LINQ Queries

1/ Use of AsNoTracking

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.

7 Tips to write better LINQ Queries in .NET

2/ Include necessary Entities and Columns

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

3/ For large data use Skip and Take

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 which will give a bad user experience, Skip and Take two integer and skips provided values and then takes next values.

7 Tips to write better LINQ Queries in .NET

4/ Use async methods

For better user experience use async methods e.g. FirstOrDefaultAsync , SingleOrDefaultAsync and ToListAsync

7 Tips to write better LINQ Queries in .NET

5/ Reduce Trips to Database

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.

7 Tips to write better LINQ Queries in .NET

6/ Use TryGetNonEnumeratedCount

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.

7 Tips to write better LINQ Queries in .NET

7/ Use of IQueryable

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.

7 Tips to write better LINQ Queries in .NET

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.

This article was originally published at https://mwaseemzakir.substack.com/ on .

Whenever you're ready, there are 3 ways I can help you:

  1. Subscribe to my youtube channel : For in-depth tutorials, coding tips, and industry insights.
  2. Promote yourself to 9,000+ subscribers : By sponsoring this newsletter
  3. Patreon community : Get access to all of my blogs and articles at one place