嵌套列表带有动态变量名称的C#

时间:2014-01-16 18:14:52

标签: c#

我有一个动态的ID集我想添加到列表列表作为列表名称,所以我可以稍后引用它们进行查找,我不知道如何设置列表名称等于ID I从数据库中拉出来。

public List<List<int>> GetAllLists(string sGuid)
{
    List<List<int>> lRespondents = new List<List<int>>();
    SqlConnection sqlCon = new SqlConnection(sqlConnString);
    StringBuilder sSQL = new StringBuilder();
    sSQL.Append("SELECT Demographic_Answers.ID FROM Demographic_Answers ");
    sSQL.Append("LEFT JOIN Demographic_Questions ON Demographic_Answers.Demogrpahic_ID = Demographic_Questions.ID ");
    sSQL.Append("WHERE Demographic_Questions.Survey_ID = '" + sGuid + "' ");
    SqlCommand sqlCom = new SqlCommand(sSQL.ToString());
    sqlCom.Connection = sqlCon;
    sqlCon.Open();
    try
    {
        SqlDataReader sqlDR = sqlCom.ExecuteReader();
        while (sqlDR.Read())
        {
            List<int> sub = new List<int>(); //Dynamic Name here sub = sqlDR.GetInt32(0).ToString
            sub = getRespondentList(sqlDR.GetInt32(0).ToString()); // Dynamic Name here. Sub should = sqlDR.GetInt32(0).ToString
        }
    }
    catch
    {

    }
    finally
    {
        sqlCon.Close();
    }

    return lRespondents;
}

代码中的名称sub应为sqlDR.GetInt32(0).ToString()。 谢谢!

1 个答案:

答案 0 :(得分:0)

我想对于快速回答感到抱歉:

    public Dictionary<int, List<int>> GetAllLists(string sGuid)
    {
        Dictionary<int, List<int>> lRespondents = new Dictionary<int, List<int>>();
        SqlConnection sqlCon = new SqlConnection(sqlConnString);
        StringBuilder sSQL = new StringBuilder();
        sSQL.Append("SELECT Demographic_Answers.ID FROM Demographic_Answers ");
        sSQL.Append("LEFT JOIN Demographic_Questions ON Demographic_Answers.Demogrpahic_ID = Demographic_Questions.ID ");
        sSQL.Append("WHERE Demographic_Questions.Survey_ID = '" + sGuid + "' ");
        SqlCommand sqlCom = new SqlCommand(sSQL.ToString());
        sqlCom.Connection = sqlCon;
        sqlCon.Open();
        int iKeyName = 0;
        try
        {
            SqlDataReader sqlDR = sqlCom.ExecuteReader();
            while (sqlDR.Read())
            {
                iKeyName = sqlDR.GetInt32(0);
                List<int> sub = new List<int>(); //Dynamic Name here sub = sqlDR.GetInt32(0).ToString
                sub = getRespondentList(sqlDR.GetInt32(0).ToString()); // Dynamic Name here. Sub should = sqlDR.GetInt32(0).ToString

                lRespondents.Add(iKeyName, sub);
            }
        }
        catch
        {

        }
        finally
        {
            sqlCon.Close();
        }

        return lRespondents;
    }