Muhammad Waseem Profile picture
Mar 12, 2023 โ€ข 8 tweets โ€ข 4 min read โ€ข Read on X
๐Ÿ’ก 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

โ€ข โ€ข โ€ข

Missing some Tweet in this thread? You can try to force a refresh
ใ€€

Keep Current with Muhammad Waseem

Muhammad Waseem Profile picture

Stay in touch and get notified when new unrolls are available from this author!

Read all threads

This Thread may be Removed Anytime!

PDF

Twitter may remove this content at anytime! Save it as PDF for later use!

Try unrolling a thread yourself!

how to unroll video
  1. Follow @ThreadReaderApp to mention us!

  2. From a Twitter thread mention us with a keyword "unroll"
@threadreaderapp unroll

Practice here first or read more on our help page!

More from @mwaseemzakir

Aug 2, 2023
Multiple ways to create middleware in .NET?

It is software assembled into an app pipeline to handle requests and responses. In simple, it gives us the facility to add additional logic before and after the HTTP request.

๐—›๐—ผ๐˜„ ๐—ฑ๐—ผ๐—ฒ๐˜€ ๐—บ๐—ถ๐—ฑ๐—ฑ๐—น๐—ฒ๐˜„๐—ฎ๐—ฟ๐—ฒ ๐˜„๐—ผ๐—ฟ๐—ธ ๐Ÿ‘‡ Image
Suppose we have three middleware in our app then for each middleware we enter two times.

First middleware logic is performed then we send it to the next middleware Image
Secondly when a request comes back after executing the next middleware

๐—ง๐—ต๐—ฟ๐—ฒ๐—ฒ ๐˜„๐—ฎ๐˜†๐˜€ ๐˜๐—ผ ๐—ฐ๐—ฟ๐—ฒ๐—ฎ๐˜๐—ฒ ๐—บ๐—ถ๐—ฑ๐—ฑ๐—น๐—ฒ๐˜„๐—ฎ๐—ฟ๐—ฒ:
- Request delegate
- By convention
- Using middleware factory

Let's dive how to implement them :
Read 8 tweets
Jun 26, 2023
What is SOLID , why we need these principles and S explained ?
It is combination of 5 design principles
S - Single Responsibility Principle (SRP)
O - Open/Closed Principle (ORP)
L - Liskov Substitution Principle (LSP)
I - Interface Segregation Principle (ISP)

Thread ๐Ÿงต๐Ÿ‘‡
D - Dependency Inversion Principle (DIP)

Why we need SOLID or its benefits
- Better Testability
- Reduces coupling
- Removes Duplication
- Increases Readability
- Increases Extensibility

SRP says :-
- A class/method should have one reason to change
- A class/method should have only one responsibility
- A class/method should be related to a single purpose

With SRP we achieve :-
- Class/method becomes smaller and cleaner
- Now in future updating the code becomes easier

Example
Read 9 tweets
Jun 20, 2023
Are you struggling to find right resources to learn C# and .NET then check these out ?

๐˜๐จ๐ฎ๐“๐ฎ๐›๐ž ๐‚๐ก๐š๐ง๐ง๐ž๐ฅ๐ฌ
- Dotnet (bit.ly/3L2CUqn)
- Edureka (bit.ly/3GLdT0k)
- Brackeys (bit.ly/3zYDMWA)
- Kudvenkat (bit.ly/3MGsSfK)๐Ÿงต๐Ÿ‘‡

#dotnet
- QuestPond (bit.ly/3N303KP)
- Nick Chapsas (bit.ly/3MKFhiz)
- Derek Banas (bit.ly/41wU9FM)
- Milan Jovanoviฤ‡ (bit.ly/3zXoPEi)
- I am Tim Corey (bit.ly/3UD0zAI)
- FreecodeCamp (bit.ly/3GMDVQC)
- NDC Conferences (bit.ly/419dPQc)
- Programming with Mosh (bit.ly/3KEuLqL)

๐Œ๐ข๐œ๐ซ๐จ๐ฌ๐จ๐Ÿ๐ญ ๐•๐ข๐๐ž๐จ ๐“๐ฎ๐ญ๐จ๐ซ๐ข๐š๐ฅ๐ฌ
- C# (bit.ly/41l0qEg)
- Desktop (bit.ly/3o82scH)
- ASP .NET (bit.ly/413blCU)
Read 10 tweets
Jun 18, 2023
Why should we use NodaTime over Default .NET DateTime ?

These are the 7 reasons

1) Lack of clarity in DateTime especially when comparing local and UTC times๐Ÿ”ฝ๐Ÿ‘‡

#dotnet Image
2) DateTimeKind property and various operations like ToUniversalTime() and ToLocalTime() can lead to unexpected results

3) DateTime does not provide a comprehensive solution for handling time zones.

#dotnet
4) TimeZoneInfo, used in .NET, relies on Windows time zone IDs, which may differ from widely used Olson (tz) identifiers

5) Unfortunately humanity didโ€™ not agree on single calendar (Gregorian, Julian, Coptic and Buddhist) , NodaTime covers this space which default lacks

#dotnet
Read 5 tweets
Jun 14, 2023
Are you using required keyword with properties ?

Since C# 11 we can add required keyword with properties , it will force to initialize that property at the time of object creation.

There are few benefits of using it
- Less chance of error
- Get rid of validation code

#dotnet Image
- If something is essential make it required

Compiler will give error if you don't initialize that property.

๐Ÿ“Œ P.S:- If you enjoy reading my posts join my weekly .NET Newsletter. Join with 3400+ :

#dotnetlnkd.in/dNHxJGRG
๐Ÿ“ŒRead these 15 awesome topics of .NET via my Newsletter.

Join 3400+ : for upcoming issues.

EP 14: 7 Awesome Features of C# (10)
EP 13: Bogus - A Fake Data Generation
EP 12: Global Exception Handling
EP 11: Default vs Autofac DI Container
EP 10: Actionโ€ฆ https://t.co/62ug4BOMVBlnkd.in/dNHxJGRG
twitter.com/i/web/status/1โ€ฆ
Read 4 tweets
May 13, 2023
What is the difference b/w field and property , what are the different types of properties in C# ?

- Properties
- Field vs Properties
- 6 Types of Properties

#dotnet Image
Properties vs Field

- Properties are first class citizens in C#
- We used properties to achieve abstraction
- Properties are implemented via accessors
- Enable developers to write code that accurately expresses their design intent
#dotnet Image
Read only Properties

- Serve the purpose of getters
- Use them for retrieving the information in DTO
- We can not set them

#dotnet Image
Read 8 tweets

Did Thread Reader help you today?

Support us! We are indie developers!


This site is made by just two indie developers on a laptop doing marketing, support and development! Read more about the story.

Become a Premium Member ($3/month or $30/year) and get exclusive features!

Become Premium

Don't want to be a Premium member but still want to support us?

Make a small donation by buying us coffee ($5) or help with server cost ($10)

Donate via Paypal

Or Donate anonymously using crypto!

Ethereum

0xfe58350B80634f60Fa6Dc149a72b4DFbc17D341E copy

Bitcoin

3ATGMxNzCUFzxpMCHL5sWSt4DVtS8UqXpi copy

Thank you for your support!

Follow Us!

:(