locked
How To Replace Loops With Lambda Expression Or LINQ To Optimize Code For Performance Purposes RRS feed

  • Question

  • User-1231523685 posted

    I have a grid with several columns and thousands of rows. The grid is populated with data from an object called DataBaseSearchResult when the user types a search word into an input box.

    When the user clicks on one or more rows the selected rows are removed from the grid, the Status column of each selected row is given a value of false so that the row will not be in the search result when the user does a search, and they are saved in the database. 

    The way I achieved the above functionalities of removing selected rows from the grid and displaying the remaining rows in the grid was I took all the ids from the RowId column (of type int) of the DataBaseSearchResult object and put them in a list. Then I took all the ids of the selected rows from the RowId column of the grid and put them in an array of type int.

    Next I used For loops to remove the selected rows from the DataBaseSearchResult object.  Finally, I rebound the DataBaseSearchResult object with the grid to display the data and I sent the ids of the selected rows to the database to be used for updating the Status field of each selected row.

    What is the most efficient way to achieve the above functionalities? If there is a way to do this efficiently using Lambda Expression Or LINQ can you please show how it is done.

    Tuesday, June 19, 2018 6:16 AM

All replies

  • User753101303 posted

    Hi,

    First usually you avoid to show thousand of rows but let the user to search FIRST so that rows are narrowed down already.

    What if other users are doing the same thing? It will change as well the "Status" ? Usually I'm using a table to keep the pk of thje current user selection for each user.

    Here it seems you are doing some kind of processing in memory. The first step could be to check which performance issue you have. Are you sure the issue is in this particular step (you check all elements against all values found in another list) or for example could it be the time needed to render that many rows. I saw once someone that thought about a db query issue while most of the time was spent actually in downloading a huge HTML page.

    Tuesday, June 19, 2018 9:08 AM
  • User-1231523685 posted

    Hi, thanks for your reply. The thousands of rows being in the grid that I had mentioned is the highest or most extreme case that my code will go through. But let's say that there are a thousand rows being displayed and the user selects 500 rows.

    The real question is how fast can For loops (one inside the other) separate the selected rows from the total number of rows vs using Lambda Expressions. My only concern at the moment is to know if Lambda Expression is faster than Loops at removing a large set of data from an existing set of data.

    Tuesday, June 19, 2018 4:12 PM
  • User303363814 posted

    A lambda is a loop.  It can't be faster than a loop because it is a loop.

    But if you have only a few thousand rows any looping should take milliseconds.  What makes you think you have a performance problem?  What have you measured?  How long does the code you have measured take to run?  Show the code.

    Standard link to Eric Lippert https://ericlippert.com/2012/12/17/performance-rant/

    Tuesday, June 19, 2018 11:24 PM
  • User1724605321 posted

    Hi gapi555,

    For performance , manual code to loop will faster ,but LINQ/lambda is better on development speed and maintainability . As @PaulTheSmith said , if you really concern the end user performance ,  just test that in your scenario with real amount of data.

    Best Regards,

    Nan Yu

    Wednesday, June 20, 2018 2:42 AM
  • User-1231523685 posted

    I have read articles that agree with you but unfortunately some really influential people on my team believe that Lambda is faster than loop for iterating through loops and I've gotten some flak for using loops for the mentioned purpose.

    Saturday, June 23, 2018 2:32 PM
  • User303363814 posted

    Don't use loops.  Keep your job.

    Monday, June 25, 2018 1:55 AM
  • User1724605321 posted

    Hi gapi555,

    To eliminate the concern,  you can test the performance with the real amount of data , and show the performance test result . But if there is no big difference in performance , LINQ/lambda is better on development speed and maintainability .

    Best Regards,

    Nan Yu

    Monday, June 25, 2018 2:23 AM