我如何在 C# 中解决这个循环?

How can I resolve this loop in c#?

我正在使用 visual studio 2010 在 c# 桌面应用程序中工作。 我想读取每条记录并为每个美国州加载一个 "street name dictionary"。 例如,我读取了第一条记录 (id=1),所以我加载 AlabamaDictionary.txt 因为这条记录来自阿拉巴马州,在加载它之后,开始另一个过程,如地址标准化等。当这个过程完成时,我读取下一条记录 (id=2) 并且我必须检查 "State" 是否相同,如果状态与以前相同,我不需要再次加载 AlabamaDictionary.txt 因为它是加载还没有开始另一个过程,比如地址标准化。完成此过程后,我读取了来自 "Arizona" 的下一条记录 (id=3)。这里状态发生变化,所以我需要加载 Arizona.txt 并重新开始另一个地址标准化过程,等等...... 我的问题是更改字典,检查何时更改。

Records
    "id" | "address"       | "state"
    1    |100 Elm St       | Alabama
    2    |300 Trawick St   | Alabama
    3    |50023 N 51st Ave | Arizona

我有下一个循环 代码: DataTable记录;

for (int i = 0; i < records.Rows.Count; i++)
{
string address = records.Rows[i][1].ToString(); 
string  state = records.Rows[i][2].ToString();
streetDictionary = state + ".txt";
if(File.Exists(streetDictionary))
{
//Here I need to identify state change, 
//so if state change , to use another dictionary and load it, 
//but if don't change it (state), I need to use the same dictionary 
//(if is load it yet)
 LoadStreetDictionary(streetDictionary);

//Process Address Standardization
StreetNameStandardization(address)
}
} 

拜托,我该如何做这个循环? 非常感谢!

在循环之前定义一个变量...

string lastState="";

然后把字典的阅读附在支票里,看看你是否还在同一个状态

if (!state.equals(lastState))
{
    // read the dictionary here

    // then...
    lastState = state;
}
// check your address here
StreetAddressStandardization();

您可以将最新状态存储在变量中,并添加每次迭代检查

string prevState = string.Empty;
   for (int i = 0; i < records.Rows.Count; i++)
    {

        string address = records.Rows[i][1].ToString(); 
        string  state = records.Rows[i][2].ToString();

    if (!state.Equals(prevSate))
    {
        streetDictionary = state + ".txt";
        if(File.Exists(streetDictionary))
        {
            //Here I need to identify state change, 
            //so if state change , to use another dictionary and load it, 
            //but if don't change it (state), I need to use the same dictionary 
            //(if is load it yet)
            LoadStreetDictionary(streetDictionary);
        }


     }

     //Process Address Standardization
     StreetNameStandardization(address)
     prevState = state; //Assigned to latest value
    } 

编辑:始终执行标准化功能。

希望我能正确理解您的要求。

String lastState = "";
for (int i = 0; i < records.Rows.Count; i++)
{
    string address = records.Rows[i][1].ToString(); 
    string  state = records.Rows[i][2].ToString();
    streetDictionary = state + ".txt";
    if(File.Exists(streetDictionary))
    {
        if (currentState != state)
            LoadStreetDictionary(streetDictionary);
        lastState = state;

        //Process Address Standardization
        StreetNameStandardization(address)
    }
}