簡體   English   中英

如何將值添加到列表數組

[英]How to add value to a List Array

我正在為“國家和城市”進行級聯下拉菜單,該菜單現在可以正常工作,但是無論選擇哪個國家/地區的用戶,我都需要將other city作為最后一個值添加到第二個下拉列表(城市DD)中。 我無法使其像正常的添加操作那樣工作。

public CascadingDropDownNameValue[] FetchCities(string knownCategoryValues, string category)
    {
        string CountryCode;
        StringDictionary strCountries = AjaxControlToolkit.CascadingDropDown.ParseKnownCategoryValuesString(knownCategoryValues);
        CountryCode = strCountries["Country"].ToString();
        con.Open();
        SqlCommand cmd = new SqlCommand("select * from Cities where Country=@Code Order by CityName ", con);
        cmd.Parameters.AddWithValue("@Code", CountryCode);
        cmd.ExecuteNonQuery();
        SqlDataAdapter dastate = new SqlDataAdapter(cmd);
        DataSet ds = new DataSet();
        dastate.Fill(ds);
        con.Close();
        List<CascadingDropDownNameValue> states = new List<CascadingDropDownNameValue>();
        foreach (DataRow dtRow in ds.Tables[0].Rows)
        {
            string StateID = dtRow["CityID"].ToString();
            string StateName = dtRow["CityName"].ToString();
            states.Add(new CascadingDropDownNameValue(StateName, StateID));
        }
        return states.ToArray();
    }

上面的代碼是示例代碼,我正在關注本教程的級聯下拉菜單

您無需觸摸代碼即可執行此操作。只需在“ Cities”表中添加新行

Country = "xxx" 
CityName = "ZZZ: other country"

然后更新您的SQL:

SqlCommand cmd = new SqlCommand("select * from Cities where Country=@Code OR Country='xxx' Order by CityName ", con);

開頭的ZZZ只是為了確保按CityName進行訂購時始終在結尾

為什么不將其作為列表傳遞給函數(我不喜歡返回列表)作為返回值。 這樣,您可以控制獲取數據庫值后列表發生的情況。

public void Fill()
{
    List<CascadingDropDownNameValue> cities = new List<CascadingDropDownNameValue>();

    FetchCities(cities, knownCategoryValues, category);

    cities.Add(new CascadingDropDownNameValue("new city", "0"));
}


public void FetchCities(List<CascadingDropDownNameValue> values, string knownCategoryValues, string category)
{
    string CountryCode;
    StringDictionary strCountries = AjaxControlToolkit.CascadingDropDown.ParseKnownCategoryValuesString(knownCategoryValues);
    CountryCode = strCountries["Country"].ToString();
    con.Open();
    SqlCommand cmd = new SqlCommand("select * from Cities where Country=@Code Order by CityName ", con);
    cmd.Parameters.AddWithValue("@Code", CountryCode);
    cmd.ExecuteNonQuery();
    SqlDataAdapter dastate = new SqlDataAdapter(cmd);
    DataSet ds = new DataSet();
    dastate.Fill(ds);
    con.Close();
    List<CascadingDropDownNameValue> states = new List<CascadingDropDownNameValue>();
    foreach (DataRow dtRow in ds.Tables[0].Rows)
    {
        string StateID = dtRow["CityID"].ToString();
        string StateName = dtRow["CityName"].ToString();
        values.Add(new CascadingDropDownNameValue(StateName, StateID));
    }
}

更新 :另一個不錯的方法是返回IEnumerable <CascadingDropDownNameValue>

    public void Fill()
    {
        List<CascadingDropDownNameValue> cities = new List<CascadingDropDownNameValue>();

        cities.AddRange(FetchCities(connectionString, knownCategoryValues, category));

        cities.Add(new CascadingDropDownNameValue("new city", "0"));
    }


    public IEnumerable<CascadingDropDownNameValue> FetchCities(string connectionString, string knownCategoryValues, string category)
    {
        StringDictionary strCountries = AjaxControlToolkit.CascadingDropDown.ParseKnownCategoryValuesString(knownCategoryValues);
        string CountryCode = strCountries["Country"].ToString();

        using (SqlConnection con = new SqlConnection(connectionString))
        using (SqlCommand cmd = new SqlCommand("select * from Cities where Country=@Code Order by CityName ", con))
        {
            cmd.Parameters.Add("@Code", SqlDbType.VarChar).Value = CountryCode;
            using (SqlDataReader reader = cmd.ExecuteReader())
                while (reader.Read())
                {
                    string StateID = reader["CityID"].ToString();
                    string StateName = reader["CityName"].ToString();
                    yield return new CascadingDropDownNameValue(StateName, StateID);
                }
        }
    }

好處是它將在需要時流式處理行並將其轉換為對象。 這將帶來的優勢是,如果您只想要前10名,它將不會從sql-server中獲得更多行。 (除了某些行緩存/塊讀取)

DataSet.Fill將在返回對象之前獲取所有行。 因此,如果該表包含+ 1m行。 它將首先創建一個包含1m行的DataTable,然后將其返回給對象。

我將連接字符串傳遞給它的原因是為了允許更多打開的記錄集。 (每個連接一個打開的記錄集)

在這種情況下, cities.AddRange將迭代所有行。 僅添加前十名將是:

var cities = FetchCities(connectionString, knownCategoryValues, category).Take(10);
cities.AddRange(cities);

祝好運。

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM