Hassan Vaezzadeh on LinkedIn: 5 Techniques to Optimized EF Query in .NET Core. (2024)

Hassan Vaezzadeh

Software Engineer | .NET Developer

  • Report this post

๐Ÿ“ข 5 Techniques to Optimized EF Query in .NET Core1๏ธโƒฃ Get fields you need onlyIt is about optimizing data retrieval by selecting only the specific fields required for a particular operation, rather than fetching entire entity objects.2๏ธโƒฃ Avoid N+1 QueriesIt occurs when your application makes one query to retrieve a set of objects, and then additional queries for each object to retrieve related data. This can lead to a significant performance bottleneck, especially when dealing with a large number of objects.3๏ธโƒฃ Using .AsNoTracking()Using AsNoTracking in Entity Framework Core is a performance optimization technique particularly useful in scenarios where you are only reading data from the database and do not intend to update or delete it.4๏ธโƒฃ Avoiding Cartesian ExplosionA cartesian explosion refers to a situation where a query unintentionally produces a disproportionately large number of records due to the way joins are handled, significantly impacting the performance and efficiency of the query.5๏ธโƒฃ Use AsSplitQuery()the AsSplitQuery() method is used to split queries that retrieve multiple related entities into separate SQL queries. This can sometimes improve performance, especially when dealing with complex queries or large datasets.#dotnetcore #efcore #ef #csharp #dotnetdeveloper

144

3 Comments

Like Comment

Mohammad Goroohi

.NET Developer | Maui Android Developer | Blazor

5mo

  • Report this comment

excellent! These EF Query optimization techniques are really useful in .NET Core. Especially the use of AsNoTracking and AsSplitQuery, which can have a significant effect on improving performance. Thanks for sharing these valuable tips!

Like Reply

1Reaction 2Reactions

Pavel Vasilevich

.Net Backend Developer

5mo

  • Report this comment

Very useful tips

Like Reply

1Reaction 2Reactions

See more comments

To view or add a comment, sign in

More Relevant Posts

  • Dipak Pakhale

    ๐Ÿ“ฑFounder at Sparkle Web | IT Services & Consultancy Expert | Empowered 30+ Entrepreneurs & Founders to Transform Digitally | Website & Mobile App Development | Boosting Client Business with 5X Growth๐Ÿš€

    • Report this post

    Understanding the ๐๐ข๐Ÿ๐Ÿ๐ž๐ซ๐ž๐ง๐œ๐ž ๐›๐ž๐ญ๐ฐ๐ž๐ž๐ง ๐ž๐š๐ ๐ž๐ซ ๐ฅ๐จ๐š๐๐ข๐ง๐  ๐š๐ง๐ ๐ฅ๐š๐ณ๐ฒ ๐ฅ๐จ๐š๐๐ข๐ง๐  ๐ข๐ง ๐„๐ง๐ญ๐ข๐ญ๐ฒ ๐…๐ซ๐š๐ฆ๐ž๐ฐ๐จ๐ซ๐ค can significantly impact your application's performance and resource usage.With ๐ž๐š๐ ๐ž๐ซ ๐ฅ๐จ๐š๐๐ข๐ง๐ , all related entities, such as products and their associated categories, are fetched upfront in a single database query. This reduces the need for additional queries when accessing related data.In contrast, ๐ฅ๐š๐ณ๐ฒ ๐ฅ๐จ๐š๐๐ข๐ง๐  delays the loading of related entities until they're explicitly accessed. While convenient, this approach can result in additional queries being executed on-demand, potentially impacting performance.Understanding when to use each loading strategy is crucial for optimizing data retrieval in Entity Framework applications.#entityframework #performanceoptimization #dataretrieval #csharp #dotnet #orm

    32

    Like Comment

    To view or add a comment, sign in

  • sudip ranabhat

    Senior .NET Developer | Software Architecture Enthusiast | .Net Core, C# ,Web API | Agile Methodologies | Research and Development

    • Report this post

    Implementing Global Query Filter in EntityFramework is a good practice, especially for soft delete.But if you are using Database First Approach, every time you do a reverse engineering , this query filter will reset.How can it be done using Database First Appraoch? Click the link below โฌ‡ https://lnkd.in/gnPxdHk3โฉ Reshare with your network if this is helpful#003 #dotnet #softwareengineering #softwaredevelopment

    EF Core Global Query Filter and Database First Approach medium.com

    9

    Like Comment

    To view or add a comment, sign in

  • Elliot One

    Senior Full Stack Engineer | Founder at XANT

    • Report this post

    Let's review Predicate and Expression in C#!Predicate<T> and Expression<Func<T, bool>> in C# are two powerful tools for data filtering.Predicate<T>:+ Is a delegate to define criteria that return a boolean to indicate whether an object meets these conditions.+ It's ideal for in-memory filtering of collections, providing a straightforward way to filter data without complex setups.Expression<Func<T, bool>>:+ Shines when working with Entity Framework Core.+ This tool allows you to build expression trees that are translated into SQL queries.+ Helps to dynamically construct complex queries that EF Core can efficiently translate into database queriesWhy Use Expressions?1. Expressions let you construct queries on-the-fly based on runtime conditions.2. By translating expression trees into SQL, you leverage the database's processing power for filtering, making it more efficient than loading all data into memory.3. Expressions enable you to write modular and reusable query logic. 4. With Expressions, you seamlessly integrate with LINQ and EF Core, making it easier to write clean and maintainable code.5. Building intricate queries with ease.Predicate<T> and Expression<Func<T, bool>> together offer a robust approach to data filtering, whether you're working with in-memory collections or querying a database.โœ… They help build more powerful, dynamic, modular, and scalable queries.โœ Share your experiences using Expressions in the comments!-- Follow Elliot One for more software engineer posts.#dotnet #efcore #softwareengineering

    • Hassan Vaezzadeh on LinkedIn: 5 Techniques to Optimized EF Query in .NET Core. (16)

    329

    35 Comments

    Like Comment

    To view or add a comment, sign in

  • Dipak Pakhale

    ๐Ÿ“ฑFounder at Sparkle Web | IT Services & Consultancy Expert | Empowered 30+ Entrepreneurs & Founders to Transform Digitally | Website & Mobile App Development | Boosting Client Business with 5X Growth๐Ÿš€

    • Report this post

    Are you curious about the differences between Database-First and Code-First approaches in Entity Framework? Let's understand it!๐ƒ๐š๐ญ๐š๐›๐š๐ฌ๐ž-๐…๐ข๐ซ๐ฌ๐ญ:Start with an existing database and generate models from it. Perfect for when your database is already set up.๐‚๐จ๐๐ž-๐…๐ข๐ซ๐ฌ๐ญ:Start with the code, then create the database. Ideal for when you want full control over your models and database design.๐–๐ก๐ข๐œ๐ก ๐ญ๐จ ๐‚๐ก๐จ๐จ๐ฌ๐ž?โžบ Database-First: Use this if your database is already built.โžบ Code-First: Choose this if you want to design your database through code.Understanding these approaches helps you pick the right one for your project. Happy coding!Stay tuned for more easy tech tips! Like, Repost, and Comment below!#dotnet #entityframework #databasefirst #codefirst #csharp

    130

    1 Comment

    Like Comment

    To view or add a comment, sign in

  • Junaid Khan

    Full Stack .Net Developer | Asp .Net Core | C# | Blazor | Web API | SQL Server | Entity Framework Core |

    • Report this post

    Code First vs Database First

    1

    Like Comment

    To view or add a comment, sign in

  • Shwetha M D

    Software Developer (.net ), immediate joiner. | ASP.net MVC | c# | ASP.net core Web API | Entity Framework | Linq |SQL |Typescript |Angular

    • Report this post

    LINQ (Language Integrated Query) in C# is a feature that lets you query and manipulate data directly within your codeusing a SQL-like syntax or method calls. It supportsquerying various data sources such as collections, databases,XML, etc., with powerful operators like Where, Select, OrderBy, and more.LINQ provides efficient querying through deferred execution and integrates seamlessly withC# for improved code readability and maintenance.Example:// Sample dataList<int> numbers = new List<int> { 1, 2, 3, 4, 5, 6, 7, 8, 9, 10 };// Query Syntaxvar evenNumbersQuery = from num in numberswhere num % 2 == 0select num;// Method Syntaxvar evenNumbersMethod = numbers.Where(num => num % 2 == 0);// Iterating through resultsforeach (var num in evenNumbersQuery){Console.WriteLine(num); // Output: 2, 4, 6, 8, 10}foreach (var num in evenNumbersMethod){Console.WriteLine(num); // Output: 2, 4, 6, 8, 10}

    2

    Like Comment

    To view or add a comment, sign in

  • Ahmed Yezdane

    Full-Stack Developer | ASP.NET Core | Vue.js | Back end Developer Dotnet | .Net Enthusiastic | Software Engineer .Net

    • Report this post

    ๐™๐™ฃ๐™™๐™š๐™ง๐™จ๐™ฉ๐™–๐™ฃ๐™™๐™ž๐™ฃ๐™œ ๐™ฌ๐™๐™–๐™ฉ ๐™Ž๐™Œ๐™‡ ๐™ฆ๐™ช๐™š๐™ง๐™ž๐™š๐™จ ๐™ฎ๐™ค๐™ช๐™ง ๐™‡๐™„๐™‰๐™Œ ๐™˜๐™ค๐™™๐™š ๐™œ๐™š๐™ฃ๐™š๐™ง๐™–๐™ฉ๐™š๐™จ ๐™˜๐™–๐™ฃ ๐™—๐™š ๐™ฉ๐™ง๐™ž๐™˜๐™ ๐™ฎNormally, the SQL commands executed by Entity Framework (EF) can appear ๐ช๐ฎ๐ข๐ญ๐ž ๐๐ข๐Ÿ๐Ÿ๐ž๐ซ๐ž๐ง๐ญ ๐Ÿ๐ซ๐จ๐ฆ ๐ญ๐ก๐ž ๐‹๐ˆ๐๐ queries that created them. This mismatch can make it difficult to analyze and debug your code.โญ EF's query tags feature comes to the rescue!This feature lets you add a short, descriptive comment directly into the generated SQL query.This comment acts as a bridge, making it easier to ๐—บ๐—ฎ๐˜๐—ฐ๐—ต ๐˜๐—ต๐—ฒ ๐—ฆ๐—ค๐—Ÿ ๐—ฏ๐—ฎ๐—ฐ๐—ธ ๐˜๐—ผ ๐˜๐—ต๐—ฒ ๐—ผ๐—ฟ๐—ถ๐—ด๐—ถ๐—ป๐—ฎ๐—น ๐—Ÿ๐—œ๐—ก๐—ค ๐—ฐ๐—ผ๐—ฑ๐—ฒp.s: check out the post about TagWithCallSite's twin:https://lnkd.in/dfXP869W#dotnet #efcore #csharp

    • Hassan Vaezzadeh on LinkedIn: 5 Techniques to Optimized EF Query in .NET Core. (29)

    40

    2 Comments

    Like Comment

    To view or add a comment, sign in

  • Marya Amouri

    Backend Developer | ASP.NET Core

    • Report this post

    #DbContext in Entity Framework Core๐Ÿท๏ธ DbContext is a component for managing entities, relationships and configuring #EFCore 's behavior.๐Ÿท๏ธ DbContext is a #DotNet class, that acts intermediate between the .NET application and the database๐Ÿท๏ธDbContext plays a crucial role in managing entities, tracking changes, and coordinating database operations.๐Ÿท๏ธKey Responsibilities of DbContext:๐Ÿ—๏ธ Tracks the state of entities (Added, Deleted, Modified, UnChanged),to generate appropriate SQL statements when saving changes to the database.๐Ÿ—๏ธ Database Interaction: DbContext provides methods to interact with the database including:โž• Querying entities.โž• Saving changes.โž• Executing raw SQL commands.โž• Managing transactions.๐Ÿ—๏ธ DbContext translates #LINQ queries written in your application into SQL queries that can be executed against the database. This translation is handled by EF Core's query pipeline.๐Ÿ—๏ธ Configuration and Relationships:DbContext allows you to configure various aspects of EF Core, including :โž• Connection settingsโž• Entity mappingsโž•Establish relationships (e.g., one-to-many, many-to-many) between entities.โž• Database initialization strategies.#EntityFrameworkCore #DbContext #ORM #DotNetDevelopment #SoftwareEngineering #backend

    16

    Like Comment

    To view or add a comment, sign in

  • Ahmed Yezdane

    Full-Stack Developer | ASP.NET Core | Vue.js | Back end Developer Dotnet | .Net Enthusiastic | Software Engineer .Net

    • Report this post

    ๐™๐™ฃ๐™™๐™š๐™ง๐™จ๐™ฉ๐™–๐™ฃ๐™™๐™ž๐™ฃ๐™œ ๐™ฌ๐™๐™–๐™ฉ ๐™Ž๐™Œ๐™‡ ๐™ฆ๐™ช๐™š๐™ง๐™ž๐™š๐™จ ๐™ฎ๐™ค๐™ช๐™ง ๐™‡๐™„๐™‰๐™Œ ๐™˜๐™ค๐™™๐™š ๐™œ๐™š๐™ฃ๐™š๐™ง๐™–๐™ฉ๐™š๐™จ ๐™˜๐™–๐™ฃ ๐™—๐™š ๐™ฉ๐™ง๐™ž๐™˜๐™ ๐™ฎNormally, the SQL commands executed by Entity Framework (EF) can appear ๐ช๐ฎ๐ข๐ญ๐ž ๐๐ข๐Ÿ๐Ÿ๐ž๐ซ๐ž๐ง๐ญ ๐Ÿ๐ซ๐จ๐ฆ ๐ญ๐ก๐ž ๐‹๐ˆ๐๐ queries that created them. This mismatch can make it difficult to analyze and debug your code.โญ EF's query tags feature comes to the rescue!This feature lets you add a short, descriptive comment directly into the generated SQL query.This comment acts as a bridge, making it easier to ๐—บ๐—ฎ๐˜๐—ฐ๐—ต ๐˜๐—ต๐—ฒ ๐—ฆ๐—ค๐—Ÿ ๐—ฏ๐—ฎ๐—ฐ๐—ธ ๐˜๐—ผ ๐˜๐—ต๐—ฒ ๐—ผ๐—ฟ๐—ถ๐—ด๐—ถ๐—ป๐—ฎ๐—น ๐—Ÿ๐—œ๐—ก๐—ค ๐—ฐ๐—ผ๐—ฑ๐—ฒ#dotnet #efcore #csharp

    • Hassan Vaezzadeh on LinkedIn: 5 Techniques to Optimized EF Query in .NET Core. (36)

    78

    8 Comments

    Like Comment

    To view or add a comment, sign in

Hassan Vaezzadeh on LinkedIn: 5 Techniques to Optimized EF Query in .NET Core. (40)

Hassan Vaezzadeh on LinkedIn: 5 Techniques to Optimized EF Query in .NET Core. (41)

3,955 followers

  • 74 Posts

View Profile

Follow

Explore topics

  • Sales
  • Marketing
  • IT Services
  • Business Administration
  • HR Management
  • Engineering
  • Soft Skills
  • See All
Hassan Vaezzadeh on LinkedIn: 5 Techniques to Optimized EF Query in .NET Core. (2024)
Top Articles
Latest Posts
Recommended Articles
Article information

Author: Dean Jakubowski Ret

Last Updated:

Views: 5947

Rating: 5 / 5 (50 voted)

Reviews: 89% of readers found this page helpful

Author information

Name: Dean Jakubowski Ret

Birthday: 1996-05-10

Address: Apt. 425 4346 Santiago Islands, Shariside, AK 38830-1874

Phone: +96313309894162

Job: Legacy Sales Designer

Hobby: Baseball, Wood carving, Candle making, Jigsaw puzzles, Lacemaking, Parkour, Drawing

Introduction: My name is Dean Jakubowski Ret, I am a enthusiastic, friendly, homely, handsome, zealous, brainy, elegant person who loves writing and wants to share my knowledge and understanding with you.