How can you achieve high performance in DataTable when searching in it?

Are there any analogs of indexes or restrictions that will allow you to search faster?

  • PrimaryKey does not work well? If id is unique, you can add a column with PrimaryKey (property) and raise a row by it. - nick_n_a
  • And what are your uzkeysy? - andreycha

2 answers 2

I would suggest not to search at all in DataTable . A good approach, it seems to me, is reading data in a background thread, fetching data from them into a list of typed structures and searching is already there. To speed up, build the necessary indices (in the simplest case, just Dictionary<string, Entity> ).

(Well, in general, it seems to me that DataTable is good for applications that look like this and not like this .)

  • Is it better to read the bare DataRow instead of reading them, push them into some class and wrap them in a List? Is there a difference between reading a DataReader and running through foreach? - iluxa1810
  • @ iluxa1810: Well, faster running through the list, and plus indexing, does not happen. If the DataTable is slow, then it is doing something wrong. Plus, you probably want to show not the contents of the table with the ID and ForeinKey, but normal data. - VladD
  • hmm ... really. Using the dictionary reduced 5 minutes of conversions, to a few seconds. And after all I set a DataLable LINQ on the DataLable, and here and without it everything is in chocolate. - iluxa1810
  • one
    @ iluxa1810: You see, suitable data structures often solve a problem much better than parallelizing and using processor power. - VladD

Stored Procedures and DataReader

  • one
    And can you write a more detailed answer? - Vadim Ovchinnikov