Caveatrob
Caveatrob

Reputation: 13277

Loading a lookup table from a database into a C# program - data structure?

I have a table full of id's,categories and weights that I need to reference in my program as I read in records that contain those categories. What is the most efficient method to read those from a database and put into a structure that I can reference?

The ID's (and possibly the names) would be unique

Data might look like:

ID,Category,Weight
1,Assignment,5
2,Test,10
3,Quiz,5
4,Review,3

Upvotes: 1

Views: 1526

Answers (3)

Pete OHanlon
Pete OHanlon

Reputation: 9146

If you're using a later version of .NET, you could always use Linq to just grab that data for you.

Upvotes: 2

Nick Josevski
Nick Josevski

Reputation: 4216

If you want to avoid a database hit to fetch static data, you can hard-code the values into a common class in your solution. A Dictionary collection would work fine here too.

The trade off of course is; 2 locations to manage for any possible future changes.

Upvotes: 0

Beep beep
Beep beep

Reputation: 19161

Your best bet is to read in your table using a DataReader, and put each row into an object containing Category and Weight, then each object into a Dictionary.

Upvotes: 2

Related Questions