adsense

Friday, September 13, 2024

C # 8 More concise Switch

 In C# 8.0, the Switch statements has become more powerful with patterns. All the  cases have turned into expressions and become more readable. 

Following code illustrates basic usage of new switch.

var action= 1;


var result = operation switch

{

    1 => "Action 1",

    2 => "Action 2",

    3 => "Action 3",

    4 => "Action 4",

    _ => "No Action  performed"

};


Console.WriteLine(result );


More information can be found here.

Chaeers,

Samitha

Sunday, September 8, 2024

EF .Net Core AsNoTracking

EF .Net Core retrieves entities from the database with racking enabled, meaning any changes made to the entity are observed and can be automatically saved back to the database.

This automatic monitory feature is not optimal when it comes to read only operations (SELECT statements). To get more information on Tracking Vs Non Tracking queries read following Microsoft article.

As an optimization for read-only queries we can AsNoTracking as shown below. Adding this will stop E EF Core tracking the results, returning in improved performance as EF Core doesn't manage the state of the entity.

var books= dbContext.Books

    .AsNoTracking()

    .ToList();

 

Cheers,

Samitha