Suppose we need to build a collection of Postal Code in which each postal code contains a collection of Cities. Each City is itself a collection of Street Addresses and each Street contains all the residents and their personal record. We are going to make this whole collection with Dictionary collection which is one of the best data structure from searching efficiency perspective. So our “Three dimensional dictionary collection” will look like:
Postal Code -> City –> Street Addresses -> Resident’s Records -> SSN, Name, Age, Gender, Occupation
In C#, we can implement the above collection in following way:
class ThreeDimensionalDictionaryDemo
{
public Dictionary<string, Dictionary<string, Dictionary<string, List<ResidentRecord>>>> PeopleCollection = new Dictionary<string, Dictionary<string, Dictionary<string, List<ResidentRecord>>>>();
public void AssemblePersonRecordByPostalCode()
{
try
{
// A data record file "RecordsFile.txt" is read.
StreamReader oStreamReader = new StreamReader("RecordsFile.txt");
string line;
string postalCode;
string city;
string streetAddress;
ResidentRecord oRecord;
while ((line = oStreamReader.ReadLine()) != null)
{
// In records file, we keep one person's record in one line and data fields are seperated by TAB
// For example:
// 933467543 John Smith 24 Male Business 21093 Timonium 2311 York Rd Suit # 44
string[] parsedItem = line.Split('\t');
// Retrieving data fields
oRecord.SSN = parsedItem[0];
oRecord.Name = parsedItem[1];
oRecord.Age = Convert.ToInt16(parsedItem[2]);
oRecord.Gender = parsedItem[3];
oRecord.Occupation = parsedItem[4];
postalCode = parsedItem[5];
city = parsedItem[6];
streetAddress = parsedItem[7];
// If we get an already existing Postal Code
if (PeopleCollection.ContainsKey(postalCode))
{
// If we get an already existing City
if (PeopleCollection[postalCode].ContainsKey(city))
{
// If we get an already existing StreetAddress
if (PeopleCollection[postalCode][city].ContainsKey(streetAddress))
{
// Person's record is added into list
PeopleCollection[postalCode][city][streetAddress].Add(oRecord);
}
// If we get a new StreetAddress
else
{
// Street Address is added and value part is newed up
PeopleCollection[postalCode][city].Add(streetAddress, new List<ResidentRecord>());
// Person's record is added into list
PeopleCollection[postalCode][city][streetAddress].Add(oRecord);
}
}
// If we get a new city
else
{
// City is added and value part is newed up
PeopleCollection[postalCode].Add(city, new Dictionary<string, List<ResidentRecord>>());
// Street Address is added and value part is newed up
PeopleCollection[postalCode][city].Add(streetAddress, new List<ResidentRecord>());
// Person's record is added into list
PeopleCollection[postalCode][city][streetAddress].Add(oRecord);
}
}
// If we get a new postal code
else
{
// Postal code is added and value part is newed up
PeopleCollection.Add(postalCode, new Dictionary<string, Dictionary<string, List<ResidentRecord>>>());
// City is added and value part is newed up
PeopleCollection[postalCode].Add(city, new Dictionary<string, List<ResidentRecord>>());
// Street Address is added and value part is newed up
PeopleCollection[postalCode][city].Add(streetAddress, new List<ResidentRecord>());
// Person's record is added into list
PeopleCollection[postalCode][city][streetAddress].Add(oRecord);
}
}
}
catch (Exception e)
{
throw e;
}
}
}
thanks for these explanations , but i have a problem i want to implement the three dimensional dictionary but my data are stored in database not in .txt file. how to proceed?
ReplyDeleteThen you need to read the data using ADO.Net from Database. You can read the data by dataReader or dataSet. For your reference here is link:
Deletehttp://www.java2s.com/Code/CSharp/Database-ADO.net/ReaddatafromDataSet.htm