Hi there,
I am very new to C# (as my explanation will show)
I currently have a program that reads lines in from a text file, and processes them in a windows form.
The items being read are products, i want to calculate the occurrences of the read in products
Say for instance,
Apple
Apple
Orange
Banana
I want to be able to take "Apple" and then search through my frequency table, to see if it exists. If it doesn't, add it, and change the occurrence to "1". If it does, just change the occurrence on that record, then next item.
At the moment i am trying to construct a crazy while loop, but i think that there must be a easier way to simply compare my string to the datatables fields.
Thanks in advance for any help given :)
Tom
Loading
Ryan AlfordPosted Sep 19, 2008, 11:05 AM
--------- Code ------------------
Dictionary
--------- Code ------------------
the "string" will be the product name, and the "int" will be the number of occurances
while reading in the data, check to see if the product exists in the dictionary...
--------- Code ------------------
if (productDictionary.ContainsKey("WhateverWasJustReadFromTheFile"))
{
// This means that the product is already added, so we increment the occurances
int occurances = 0;
productDictionary.TryGetValue("WhateverWasJustReadFromTheFile", out occurances);
productDictionary["WhateverWasJustReadFromTheFile"] = occurances + 1;
}
else
{
// This means that the product was not in the list, so we add it
// and set it's occurances to 1
productDictionary.Add("WhateverWasJustReadFromTheFile", 1);
}
--------- Code ------------------
Sadaqat HussainPosted Sep 19, 2008, 2:26 AM
As you are new to c-sharp the easy way for you. First of all read all your data from file and while reading the data you have to maintain as dictionary where you will store the information about particular product and its occurrence. After you will write query to update table where you will check if that does not exists then insert into data other update the field.
Hope full answer the question.