_ json_3 layer format data source DataSet

For example, the following method, get a view of the data contained Province City shop

public DataSet GetData()
        {
            try
            {
                StringBuilder sql = new StringBuilder("SELECT PROVINCE_ID,PROVINCE_NAME,CITY_ID,CITY_NAME,DEALER_ID,DEALER_NAME ");
                sql.Append(" FROM VIEW_P_C_Data  ");
                sql.Append(" WHERE  1=1 order by PROVINCE_NAME,CITY_NAME");
                DataSet ds = dbManager.Query(sql.ToString());
                return ds;
            }
            catch (Exception ex)
            {
                throw new Exception(ex.Message);
            }
        }

First, the acquired data DataSet ds = GetData ();

声明:HashSet<string> vehicle_p = new HashSet<string>();

Then the cycle ds

foreach (DataRow row in ds.Tables[0].Rows)
{

}

Make a copy cycle in judgment, because the province is repeated, the city also. Many relationship. So when the same province in different cities, to add a json structure as long as the provinces

bool exist_province = vehicle_p.Add(row["PROVINCE_NAME"].ToString());
bool exist_city = vehicle_p.Add(row["CITY_ID"].ToString());

// when the province does not exist, for the first time added

if (exist_province)
{

}else{

// When the same province in different cities,
IF (exist_city)
{

}else{

// same city, different shops

}

}

 

PS: all code

public List<Text_Drive_Dealer> GetAllDealer()
        {
            List<Text_Drive_Dealer> list = new List<Text_Drive_Dealer>();
            DataSet ds = GetData();
            HashSet<string> vehicle_p = new HashSet<string>();
            Text_Drive_Dealer t_dealer = null;
            List<Text_City> list_vm = null;
            Text_City vm = null;
            foreach (DataRow row in ds.Tables[0].Rows)
            {
                bool exist_province = vehicle_p.Add(row["PROVINCE_NAME"].ToString());
                bool exist_city = vehicle_p.Add(row["CITY_ID"].ToString());
               
                if (exist_province)
                {
                    //当省份不存在时,第一次添加
                    t_dealer = new Text_Drive_Dealer();
                    t_dealer.ProvinceID = row["PROVINCE_ID"].ToString();
                    t_dealer.ProvinceName = row["PROVINCE_NAME " ] .ToString (); 

                    // first city to add 
                    vm = new new Text_City (); 
                    vm.CityID = Row [ " city_id " ] .ToString (); 
                    vm.CityName = Row [ " CITY_NAME " ] .ToString ( );
                     // first added to the city's special shops 
                    Text_dealer td = new new Text_dealer (); 
                    td.DealerID = Row [ " DEALER_ID " ] .ToString (); 
                    td.DealerName = Row [ "DEALER_NAME"].ToString();
                    List<Text_dealer> list_td = new List<Text_dealer>();
                    list_td.Add(td);
                    vm.text_dealer = list_td;
                    list_vm = new List<Text_City>();
                    list_vm.Add(vm);

                    t_dealer.TextDrive_City = list_vm;   
                    list.Add(t_dealer);
                }
                else
                {
                    
                    if (exist_city)
                    {
                        //第一次添加城市跟特约店
                        vm = new Text_City();
                        vm.CityID = row["CITY_ID"].ToString();
                        vm.CityName = row["CITY_NAME"].ToString();

                        Text_dealer td = new Text_dealer();

                        td.DealerID = row["DEALER_ID"].ToString();
                        td.DealerName = row["DEALER_NAME"].ToString();

                        List<Text_dealer> list_td = new List<Text_dealer>();
                        list_td.Add(td);
                        vm.text_dealer = list_td;
                        t_dealer.TextDrive_City.Add(vm);
                    }
                    else
                    {
                        //已有城市,添加特约店
                        //Text_City vm = new Text_City();
                        Text_dealer td = new Text_dealer();
                        td.DealerID = row["DEALER_ID"].ToString();
                        td.DealerName = row["DEALER_NAME"].ToString();
                        vm.text_dealer.Add(td);
                        //t_dealer.TextDrive_City.Find().text_dealer=;
                       
                    }
                }
            }
            return list;
        }

 

Guess you like

Origin www.cnblogs.com/ljh19/p/11057894.html