0
SearchList2 = SearchList1.Where(item => item.name.StartsWith(searchValue)).ToList();

I want to search through a list fast and store the result in another list like the previous code.

Note: the SearchList1 has large numbers of items

  • 3
    You already show the solution. – Ralf Dec 04 '22 at 20:28
  • Why do you think it's slow? what's your measurement? – Charles Han Dec 04 '22 at 20:37
  • 1
    There are performance tradeoffs available if the `Collection` doesn't need to be a `List`, but without knowing a lot more about the data and how it is used it's hard to choose wisely. A `Dictionary` may perform that search more efficiently, but not handle other actions as efficiently as a `List`. – HABO Dec 04 '22 at 20:37
  • ’Mr Charles Han I use this code with the small list and it worked fast , but with large list it is not , I measure that by displaying the result in interface It takes a long time – Haneen Al-fakhry Dec 04 '22 at 20:48
  • Have you tried turning the list into a HashSet and done the search? – linkedby Dec 04 '22 at 20:55
  • 2
    Please define large. It could be 100 or 1,000,000,000.... and where does `SearchList2` come from? You're doing text stuff here (possibly in the UI component) which in inherently slow! Without context no one can really answer your question. – MrC aka Shaun Curtis Dec 04 '22 at 20:57
  • Have you considered using HashSet. see this answer https://stackoverflow.com/questions/150750/hashset-vs-list-performance – Ceemah Four Dec 04 '22 at 22:37
  • 1
    Maybe it is not about searching, but more about displaying the result. You tagged this question as Blazor related. When you have UI with lots of elements, UI updates tends to be slow in Blazor (WASM). Try to measure "real" search with stopwatch class and compare it to times you are experiencing. – Alamakanambra Dec 05 '22 at 20:34

0 Answers0