Click here to Skip to main content
15,901,373 members
Please Sign up or sign in to vote.
3.00/5 (2 votes)
See more:
Hi Sir,

Good day to all.
I need help on how could I adjust the loop and my dictionary collection to quit it on my specified criteria. my "mainArea" field must be terminated if is not equal to cs.CL1, I had filtered-out already like this code:
C#
if (mainArea.Trim() == cs.CL1)

and yet it was successful, but the problem is, it keeps on repeating on a foreach loop which I acquired on my dictionary statetment. Please help me sir on this one. Your help is greatly and highly appreciated. Thank you sir.

Here's my updated code:

C#
bool clustFlag = false;


// Sort Cluster name per MainArea
var list3g= ClustDict.ClusterDict3G_CL1.Keys.ToList();
list3g.Sort();

var listgbb = ClustDict.ClusterDictGBB_CL1.Keys.ToList();
listgbb.Sort();

var listdl = ClustDict.ClusterDictDL_CL1.Keys.ToList();
listdl.Sort();

var listwx = ClustDict.ClusterDictWX_CL1.Keys.ToList();
listwx.Sort();


// Sorting dictionary values
//var items = from k in ClustDict.dictClusterName.Keys
//            orderby ClustDict.dictClusterName[k] ascending
//            select k;

var lstSortClusterName = ClustDict.dictClusterName.Keys.ToList();
lstSortClusterName.Sort();

Dictionary<string,> dictClustName = new Dictionary<string,>();
TextWriter twx = new StreamWriter(clustOutFile + "AreaCluster.csv", true, System.Text.Encoding.Default);

foreach (var key in lstSortClusterName)
{
    dictClustName.Add(key, ClustDict.dictClusterName[key]);
    twx.WriteLine(key + "," + ClustDict.dictClusterName[key]);
}
twx.Close();
twx.Dispose();
string outHandler = "", blankOutput = "" ;

int iCtr = 0;
bool flagLock = false;
string Lock = String.Empty;
tw.WriteLine(cs.CL1); // First row
	tw.WriteLine(cs.CL1); // First row
foreach (string mainArea in dictClustName.Values)
{
	// this clustName field acquired from dictionary collection is in sorted order
	// I think the problem is on this one, because it keeps repeating
	// because even the clustName is changed, the mainArea is always has a value of cs.CL1
    foreach (string clustName in dictClustName.Keys)  
    {
        // This mainArea field is in random order (not sorted)
        if (mainArea.Trim() == cs.CL1)
        {
            iCtr++;
            // 3G
            foreach (var key in list3g)
            {
                if (clustName.Trim() == key.Trim())
                {

                    outHandler = ClustDict.ClusterDict3G_CL1[key].ToString();
                    clustFlag = true;
                    break;
                }
            }

            // If found with value
            if (clustFlag)
            {
            	
                tw.Write(clustName + outHandler);
                clustFlag = false;
            }
            else if (!clustFlag)
            {
               // If no value found, must fill the blank data (blankoutput variable) to csv file
               // The loop must be running until no more value found in dictionary above
                string strMainArea = dictClustName[clustName].ToString();
                if (strMainArea == cs.CL1)
                {
                    //Dummy blank
                    blankOutput = " " + "," + clustName + "," + "0" + "," + "0" + "," +
                                       "0" + "," + "0" + "," +
                                       "0%" + "," + "0%" + "," + TAG.G3 + ",";
                    tw.Write(clustName + blankOutput);
                }
            }
        }
     }
  }tw.Close(); tw.Dispose();


// sample output which repeated the previous result (first record is BATAAN, the last record is SUBIC/ZAMBALES)
// as you can see, BATAAN was shown again on the second loop under the mainArea of cs.CL1
// See my dictionary collection statement above.
I don't know on how could I filter the mainArea, which I found out that is not changing on dictionary even the cluster field is not under on particular value of mainArea field. Please help me on this sir. Thank you.


BATAAN 	BATAAN	0	0	0	0	0%	0%	3G
BULACAN 1A 	BULACAN 1A	0	0	0	0	0%	0%	3G
BULACAN 1B 	BULACAN 1B	0	0	0	0	0%	0%	3G
BULACAN 2A 	BULACAN 2A	0	0	0	0	0%	0%	3G
BULACAN 2B 	BULACAN 2B	0	0	0	0	0%	0%	3G
BULACAN 3 	BULACAN 3	0	0	0	0	0%	0%	3G
MIDDLE PAMPANGA 	MIDDLE PAMPANGA	0	0	0	0	0%	0%	3G
PAMPANGA 1 (LOWER) 	PAMPANGA 1 (LOWER)	0	0	0	0	0%	0%	3G
PAMPANGA 2 (MIDDLE) 	PAMPANGA 2 (MIDDLE)	0	0	0	0	0%	0%	3G
PAMPANGA 3 (UPPER) 	PAMPANGA 3 (UPPER)	0	0	0	0	0%	0%	3G
PAMPANGA 4 (UPPER 2) 	PAMPANGA 4 (UPPER 2)	0	0	0	0	0%	0%	3G
SUBIC  ZAMBALES 	SUBIC  ZAMBALES	0	0	0	0	0%	0%	3G
BATAAN 	BATAAN	0	0	0	0	0%	0%	3G
BULACAN 1A 	BULACAN 1A	0	0	0	0	0%	0%	3G
BULACAN 1B 	BULACAN 1B	0	0	0	0	0%	0%	3G
BULACAN 2A 	BULACAN 2A	0	0	0	0	0%	0%	3G
BULACAN 2B 	BULACAN 2B	0	0	0	0	0%	0%	3G
BULACAN 3 	BULACAN 3	0	0	0	0	0%	0%	3G
MIDDLE PAMPANGA 	MIDDLE PAMPANGA	0	0	0	0	0%	0%	3G
PAMPANGA 1 (LOWER) 	PAMPANGA 1 (LOWER)	0	0	0	0	0%	0%	3G
PAMPANGA 2 (MIDDLE) 	PAMPANGA 2 (MIDDLE)	0	0	0	0	0%	0%	3G
PAMPANGA 3 (UPPER) 	PAMPANGA 3 (UPPER)	0	0	0	0	0%	0%	3G
PAMPANGA 4 (UPPER 2) 	PAMPANGA 4 (UPPER 2)	0	0	0	0	0%	0%	3G
SUBIC  ZAMBALES 	SUBIC  ZAMBALES	0	0	0	0	0%	0%	3G
Posted
Updated 24-May-11 15:57pm
v4
Comments
yesotaso 24-May-11 21:57pm    
Added pre tags.

1 solution

It is difficult to predict the error from this code.But you can try the following logic.

C#
foreach (string clustName in dictClustName.Keys)
{    
    if (mainArea.Trim() == cs.CL1)
    {
       // ...
    }
    else
      break;
}


[EDITED]
Please look how to use Dictionary.
http://www.dotnetperls.com/dictionary-keys[^]
 
Share this answer
 
v3
Comments
Silver Lightning 24-May-11 21:49pm    
Thanks sir Wonde,
I already updated my code above, can you help me sir on my dictionary statement above, which the mainArea field is not changing even the cluster field key is changed and it's value where it was the mainArea is no more cs.CL1. I already indicated some comment on my updated code above. Thank you in advance sir. Please help me
Wonde Tadesse 24-May-11 22:30pm    
Answer updated.
Silver Lightning 24-May-11 23:08pm    
thanks sir Wonde,
I already did that but still the same output resulted.
Here's my updated code inside the foreach loop
foreach (string clustName in dictClustName.Keys)
{

if (dictClustName[clustName].ToString() == cs.CL1)
//if (mainArea.Trim() == cs.CL1)
{
iCtr++;

and also filtered if clustFlag is false

else if (!clustFlag)
{
string strMainArea = dictClustName[clustName].ToString();
//if (strMainArea == cs.CL1)
if (dictClustName.ContainsValue(cs.CL1))
{
string cValueMainArea = dictClustName[clustName];
if (cValueMainArea.Trim() == cs.CL1)
{
//Dummy blank
blankOutput = " " + "," + clustName + "," + "0" + "," + "0" + "," +
"0" + "," + "0" + "," +
"0%" + "," + "0%" + "," + TAG.G3 + ",";
tw.Write(clustName + blankOutput);
}
}

I don't where could I filter more or where I could control foreach loop. Please help me sir. Thank you in advance.
Silver Lightning 24-May-11 23:19pm    
Hi Sir Wonde,
I made it. I only removed the outer foreach loop in mainarea it it's done.
Thanks for your help and concern sir.
God speed.. 'till next time and thank you codeproject.com
=)

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900