adsense

Sunday, November 10, 2024

EF Core AsSplitQuery

 AsSplitQuery will perform separate queries instead of complex joins when returning data from multiple tables. 

When using a single query result with data from multiple tables,  there is a probability of   “cartesian explosion” of duplicate data across many columns and rows.


e.g

  var result =dbContext.Student.Where(s=> s.Id == id)

      .Include(x => x.Books)

      .ThenInclude(x => x.Overdues)

      .AsSplitQuery();


Cheers,

Samitha

Monday, October 14, 2024

Assign an empty string if the value is null in Linq

 If you ever wanted to assign an empty string to a value returned from a Linq query, use ?? (null-coalescing operator) to return Empty string in case of null


var qry= from row in context.Table

select new {  FieldValue = (field.Value ?? string.Empty) };


Cheers,

Samitha

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

Sunday, August 18, 2024

Enumerable Check empty

 When working with Enumerable in .Net, there is always a possibility of returning an empty Enumurable if no match is found. Consider the following code.

private IEnumarable<Book> GetBook(string name)

{

   if (string.IsNullOrEmpty(name))

     return Enumerable.Empty<Book>();


  return _dbContext.Book.Where(b => b.Name.ToLower().Contains(name)); 

}

In the above piece of code  Enumerable.Empty method is used to check the null name and return an empty book rather than returning null which can cause runtime exception if  the name is not  supplied. This is considered as defensive code practice.


Cheers,

Samitha

Sunday, July 21, 2024

: The imported project Microsoft.TextTemplating.targets" was not found

 When building Visual studio you might encounter the error MSB4019: The imported project Microsoft.TextTemplating.targets" was not found.


To fix this error

  • Open the csproj in editor
  • Find following 
        <Import Project="$(MSBuildExtensionsPath)\Microsoft\VisualStudio\v16.0\TextTemplating\Microsoft.TextTemplating.targets" />

  • Change it to following and save
"$(MSBuildExtensionsPath)\Microsoft\VisualStudio\v$(VisualStudioVersion)\TextTemplating\Microsoft.TextTemplating.targets"

  • Reload the project and build

Cheers,
Samitha

Saturday, June 29, 2024

Entityframework Extensions

Entityframework Extensions  can be used to  improve your performance dramatically.


You can  perform: following operations easily.

  • BulkSaveChanges
  • BulkInsert
  • BulkUpdate
  • BulkDelete
  • BulkMerge 

Read more at https://entityframework-extensions.net/

Cheers,

Samitha