Skip to content
  • Categories
  • Recent
  • Tags
  • Popular
  • World
  • Users
  • Groups
Skins
  • Light
  • Cerulean
  • Cosmo
  • Flatly
  • Journal
  • Litera
  • Lumen
  • Lux
  • Materia
  • Minty
  • Morph
  • Pulse
  • Sandstone
  • Simplex
  • Sketchy
  • Spacelab
  • United
  • Yeti
  • Zephyr
  • Dark
  • Cyborg
  • Darkly
  • Quartz
  • Slate
  • Solar
  • Superhero
  • Vapor

  • Default (No Skin)
  • No Skin
Collapse
Code Project
  1. Home
  2. Web Development
  3. ASP.NET
  4. Which one of the following codes has better performance in ASP.NET Core?

Which one of the following codes has better performance in ASP.NET Core?

Scheduled Pinned Locked Moved ASP.NET
asp-netcsharpdotnetregexperformance
2 Posts 2 Posters 25 Views 1 Watching
  • Oldest to Newest
  • Newest to Oldest
  • Most Votes
Reply
  • Reply as topic
Log in to reply
This topic has been deleted. Only users with topic management privileges can see it.
  • A Offline
    A Offline
    Alex Dunlop
    wrote on last edited by
    #1

    I use repository pattern in my project. Which coding strategy is better? 1)

    var messages = _messageRepository.GetAllMessages().Where(x => x.senderId == myId).ToList();

    //Filtering is done in Repository
    var messages = _messageRepository.GetAllMessages(myId).ToList();

    A 1 Reply Last reply
    0
    • A Alex Dunlop

      I use repository pattern in my project. Which coding strategy is better? 1)

      var messages = _messageRepository.GetAllMessages().Where(x => x.senderId == myId).ToList();

      //Filtering is done in Repository
      var messages = _messageRepository.GetAllMessages(myId).ToList();

      A Offline
      A Offline
      Afzaal Ahmad Zeeshan
      wrote on last edited by
      #2

      EF Core is smart enough to run the ToList after the entire SQL query has been built. I am guessing the GetAllMessages part applies the filtering to the query as well; if so, they're the same in the same query builder. You need to understand two parts here. First, EF Core builds a single query that is sent to the database engine. The fluent way to build a query helps you apply all sorts of ORDER BYs and WHEREs to the SQL query; including any JOINs. Then, the database query planner kicks in and prepares how to capture the data. Database query plan will always try to find the optimum way to execute the SQL commands and return the results. Since query plans are highly based on databases themselves, I would link a Wikipedia article: [Query plan - Wikipedia](https://en.wikipedia.org/wiki/Query\_plan), you can search for the documentation per your database engine. Now, the performance would take a toll if you are filtering on the server side and not on the database side. What I mean is, that you capture the list of the records from the database and then filter them in the memory of the server process. Boy, that'd hurt a lot. Applies to modern async-based EF-only: Finally, the performance can be improved (in terms of number of requests per second) if you would use ToListAsync() instead of ToList(), where the later is a synchronous operation and would block the thread. [DbContext Lifetime, Configuration, and Initialization - EF Core | Microsoft Docs](https://docs.microsoft.com/en-gb/ef/core/dbcontext-configuration/#avoiding-dbcontext-threading-issues) [Efficient Querying - EF Core | Microsoft Docs](https://docs.microsoft.com/en-us/ef/core/performance/efficient-querying) [Performance Diagnosis - EF Core | Microsoft Docs](https://docs.microsoft.com/en-us/ef/core/performance/performance-diagnosis?tabs=simple-logging%2Cload-entities)

      The shit I complain about It's like there ain't a cloud in the sky and it's raining out - Eminem ~! Firewall !~

      1 Reply Last reply
      0
      Reply
      • Reply as topic
      Log in to reply
      • Oldest to Newest
      • Newest to Oldest
      • Most Votes


      • Login

      • Don't have an account? Register

      • Login or register to search.
      • First post
        Last post
      0
      • Categories
      • Recent
      • Tags
      • Popular
      • World
      • Users
      • Groups