patterncsharpMinor
Searching for categories and storing them
Viewed 0 times
searchingandforthemcategoriesstoring
Problem
This checks each row for a category. It stores ONE version of the category, ignore duplications. It then counts how many times a category appears.
There are a lot of
There are a lot of
for loops and a lot of table-checking. This takes time as the table is quite large. If it's possible, I would like to incorporate some of the loops to reduce time spent.public partial class Stats : Form
{
public Stats(ref TabControl languageTabs)
{
InitializeComponent();
for (int i = 0; i categories = new List();
int count = 0;
for (int j = 0; j result = categories.FindAll(
delegate(string b)
{
return b == dgv.Rows[j].Cells[1].Value.ToString();
}
);
// if we don't have a unique ID then add it
if (result.Count == 0)
{
categories.Add(dgv.Rows[j].Cells[1].Value.ToString());
}
}
int[] categoryCount = new int[categories.Count];
foreach (string cat in categories)
{
for (int j = 0; j < dgv.RowCount; j++)
{
if (dgv.Rows[j].Cells[1].Value.ToString() == cat)
{
categoryCount[count]++;
}
}
count++;
}
int countTwo = 0;
foreach (string cat in categories)
{
treeView1.Nodes[i].Nodes[2].Nodes.Add(cat + ": " + categoryCount[countTwo].ToString());
countTwo++;
}
}
}Solution
There are two pairs of loops that are the same and can be combined like this:
Note that the categoryCount array is no longer ncessary, and add Saeed's answer and you get:
Then, assuming that
int charCount = 0;
List categories = new List();
for (int j = 0; j result = categories.FindAll(
delegate(string b)
{
return b == dgv.Rows[j].Cells[1].Value.ToString();
}
);
// if we don't have a unique ID then add it
if (result.Count == 0)
{
categories.Add(dgv.Rows[j].Cells[1].Value.ToString());
}
}
treeView1.Nodes.Add(languageTabs.TabPages[i].Text.ToUpper() + " Statistics");
treeView1.Nodes[i].Nodes.Add("Total Number Of Strings: " + dgv.RowCount); // might need to remove '--' from count
treeView1.Nodes[i].Nodes.Add("Total Number Of Chars: " + charCount);
treeView1.Nodes[i].Nodes.Add("Total Number Of Strings and Chars per Category: ");
int[] categoryCount = new int[categories.Count];
int count = 0;
foreach (string cat in categories)
{
for (int j = 0; j < dgv.RowCount; j++)
{
if (dgv.Rows[j].Cells[1].Value.ToString() == cat)
{
categoryCount[count]++;
}
}
treeView1.Nodes[i].Nodes[2].Nodes.Add(cat + ": " + categoryCount[count].ToString());
count++;
}Note that the categoryCount array is no longer ncessary, and add Saeed's answer and you get:
int charCount = 0;
List categories = new List();
for (int j = 0; j x == dgv.Rows[j].Cells[1].Value.ToString()))
categories.Add(dgv.Rows[j].Cells[1].Value.ToString());
}
treeView1.Nodes.Add(languageTabs.TabPages[i].Text.ToUpper() + " Statistics");
treeView1.Nodes[i].Nodes.Add("Total Number Of Strings: " + dgv.RowCount); // might need to remove '--' from count
treeView1.Nodes[i].Nodes.Add("Total Number Of Chars: " + charCount);
treeView1.Nodes[i].Nodes.Add("Total Number Of Strings and Chars per Category: ");
foreach (string cat in categories)
{
int count = 0;
for (int j = 0; j < dgv.RowCount; j++)
{
if (dgv.Rows[j].Cells[1].Value.ToString() == cat)
{
count++;
}
}
treeView1.Nodes[i].Nodes[2].Nodes.Add(cat + ": " + count.ToString());
}Then, assuming that
dgv.Rows implements IEnumerable you can do this to the second loop:foreach (string cat in categories)
{
int count = dgv.Rows.Where(r => r.Cells[1].Value.ToString() == cat).Count();
treeView1.Nodes[i].Nodes[2].Nodes.Add(cat + ": " + count.ToString());
}Code Snippets
int charCount = 0;
List<string> categories = new List<string>();
for (int j = 0; j < dgv.RowCount; j++)
{
charCount += (int)dgv.Rows[j].Cells[4].Value;
// look at every category in the dgv and see if we have a unique value in a container
List<string> result = categories.FindAll(
delegate(string b)
{
return b == dgv.Rows[j].Cells[1].Value.ToString();
}
);
// if we don't have a unique ID then add it
if (result.Count == 0)
{
categories.Add(dgv.Rows[j].Cells[1].Value.ToString());
}
}
treeView1.Nodes.Add(languageTabs.TabPages[i].Text.ToUpper() + " Statistics");
treeView1.Nodes[i].Nodes.Add("Total Number Of Strings: " + dgv.RowCount); // might need to remove '--' from count
treeView1.Nodes[i].Nodes.Add("Total Number Of Chars: " + charCount);
treeView1.Nodes[i].Nodes.Add("Total Number Of Strings and Chars per Category: ");
int[] categoryCount = new int[categories.Count];
int count = 0;
foreach (string cat in categories)
{
for (int j = 0; j < dgv.RowCount; j++)
{
if (dgv.Rows[j].Cells[1].Value.ToString() == cat)
{
categoryCount[count]++;
}
}
treeView1.Nodes[i].Nodes[2].Nodes.Add(cat + ": " + categoryCount[count].ToString());
count++;
}int charCount = 0;
List<string> categories = new List<string>();
for (int j = 0; j < dgv.RowCount; j++)
{
charCount += (int)dgv.Rows[j].Cells[4].Value;
if (!categories.Any(x=>x == dgv.Rows[j].Cells[1].Value.ToString()))
categories.Add(dgv.Rows[j].Cells[1].Value.ToString());
}
treeView1.Nodes.Add(languageTabs.TabPages[i].Text.ToUpper() + " Statistics");
treeView1.Nodes[i].Nodes.Add("Total Number Of Strings: " + dgv.RowCount); // might need to remove '--' from count
treeView1.Nodes[i].Nodes.Add("Total Number Of Chars: " + charCount);
treeView1.Nodes[i].Nodes.Add("Total Number Of Strings and Chars per Category: ");
foreach (string cat in categories)
{
int count = 0;
for (int j = 0; j < dgv.RowCount; j++)
{
if (dgv.Rows[j].Cells[1].Value.ToString() == cat)
{
count++;
}
}
treeView1.Nodes[i].Nodes[2].Nodes.Add(cat + ": " + count.ToString());
}foreach (string cat in categories)
{
int count = dgv.Rows.Where(r => r.Cells[1].Value.ToString() == cat).Count();
treeView1.Nodes[i].Nodes[2].Nodes.Add(cat + ": " + count.ToString());
}Context
StackExchange Code Review Q#6592, answer score: 5
Revisions (0)
No revisions yet.