在C#中需要退出循环和字典收集方面的帮助 [英] Need help on quitting loop and dictionary collection in C#

查看:110
本文介绍了在C#中需要退出循环和字典收集方面的帮助的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

您好,先生,

祝大家有美好的一天.
我需要有关如何调整循环和字典集合以按指定条件退出的帮助.如果我的"mainArea"字段不等于cs.CL1,则必须终止,我已经像以下代码一样过滤掉了:

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:

if (mainArea.Trim() == cs.CL1)


虽然它成功了,但是问题是,它继续在我在字典语句中获取的foreach循环上重复.先生,请帮助我.非常感谢您的帮助.谢谢您,先生.

这是我更新的代码:


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:

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.

我不知道该怎么做我过滤了mainArea,发现簇上的值不在mainArea字段的特定值下,字典上的值也没有变化.请帮我这个先生.谢谢.


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

推荐答案

很难从此代码中预测错误.但是您可以尝试以下逻辑.

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

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




请查看如何使用字典.
http://www.dotnetperls.com/dictionary-keys [




Please look how to use Dictionary.
http://www.dotnetperls.com/dictionary-keys[^]


这篇关于在C#中需要退出循环和字典收集方面的帮助的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆