Question

I'm trying to implement a solution that stores every product from a shop, and then allows me to search them either by ID (an integer index), description (string) or barcode (string).

The idea I came up with is to have a sorted list with products ID as keys and Product objects as value, this would allow me to search quickly by ID and also implement search by "description" (getting every item's "description" property and running some kind of string match)

For barcode search (with would be unique for every product) a dictionary which would have the barcode itself as key and the product ID as value

SortedList<uint,Product> main;
Dictionary<string,uint> barcode;

But I'm not so sure if it's the right approach, because I would have to go trough every item from main to search by description. Also, If I'd want to list every Product with its barcode, I would have to either duplicate information and include the barcode inside the Product properties OR go trough every dictionary key-value pair and and figure out how to send each barcode with its corresponding product.

Is there another way to do this?

Was it helpful?

Solution

Is there another way to do this?

Yes. Use your database.

You really don't want a listed, sorted or otherwise, of a hundred million Products sitting in memory on your computer!

Leave the vast majority of the data in your database, which is built to handle stuff like this and let it do the heavy lifting.

  • Searching a table by its unique ID should always be efficient.
  • Searching by Barcode should be OK, as long as that field is indexed.
  • Searching by Product Description might be more "challenging", because Users "always" want to search for something in the middle of a field, which you cannot usefully index for.
Licensed under: CC-BY-SA with attribution
scroll top