Last post Jun 25, 2018 02:23 AM by Nan Yu
Jun 19, 2018 06:16 AM|gapi555|LINK
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
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.
Jun 19, 2018 09:08 AM|PatriceSc|LINK
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.
Jun 19, 2018 04:12 PM|gapi555|LINK
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.
Jun 19, 2018 11:24 PM|PaulTheSmith|LINK
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/
Jun 20, 2018 02:42 AM|Nan Yu|LINK
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.
Jun 23, 2018 02:32 PM|gapi555|LINK
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.
Jun 25, 2018 01:55 AM|PaulTheSmith|LINK
Don't use loops. Keep your job.
Jun 25, 2018 02:23 AM|Nan Yu|LINK
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 .