碰到if解决不了的判断,两次for循环

之前不知道把(t.Substring(0, 2) == town.Substring(0, 2)) 和(s.Substring(0, 2) == town.Substring(0, 2)) 放到一起判断会还是会漏掉数据,后来调试发现不是代码执行问题,而是这个判断本来就不完整,有的数据读取先后不一样就会漏掉,所以两次循环保证全部取到了.

for (int i = 0; i < ds.Tables[0].Rows.Count; i++)
            {
                string a = ds.Tables[0].Rows[i]["市"].ToString().Trim();
                string c = ds.Tables[0].Rows[i]["县/区"].ToString();
                string t = ds.Tables[0].Rows[i]["乡镇/街道"].ToString().Trim();
                string s = ds.Tables[0].Rows[i]["站名"].ToString().Trim();
                string n = ds.Tables[0].Rows[i]["区站号"].ToString().Trim();
                if (t == "")
                    continue;
                if (a.Substring(0, 2) == area.Substring(0, 2))
                {
                    if (t.Substring(0, 2) == town.Substring(0, 2))
                        return n;
                }
            }
            for (int i = 0; i < ds.Tables[0].Rows.Count; i++)
            {
                string a = ds.Tables[0].Rows[i]["市"].ToString().Trim();
                string c = ds.Tables[0].Rows[i]["县/区"].ToString();
                string t = ds.Tables[0].Rows[i]["乡镇/街道"].ToString().Trim();
                string s = ds.Tables[0].Rows[i]["站名"].ToString().Trim();
                string n = ds.Tables[0].Rows[i]["区站号"].ToString().Trim();
                if (t == "")
                    continue;
                if (a.Substring(0, 2) == area.Substring(0, 2))
                {
                    if (s.Substring(0, 2) == town.Substring(0, 2))
                        return n;
                }
            }

当判断条件分开时就会依次执行不用担心漏掉数据了

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值