Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/csharp/287.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
C# 在c中从两个方法添加列表#_C#_List_Add - Fatal编程技术网

C# 在c中从两个方法添加列表#

C# 在c中从两个方法添加列表#,c#,list,add,C#,List,Add,我正在尝试将getRecords列表2添加到getRecords列表中。由于某些原因,在getRecords上,处理它需要很长时间,当我调用它时会超时 public static List<ListA> getRecords2(string id) { List<ListA> listOfRecords = new List<ListA>(); using (SqlConnection con = SqlConnect.GetDBConnectio

我正在尝试将getRecords列表2添加到getRecords列表中。由于某些原因,在getRecords上,处理它需要很长时间,当我调用它时会超时

public static List<ListA> getRecords2(string id)
{
   List<ListA> listOfRecords = new List<ListA>();
   using (SqlConnection con = SqlConnect.GetDBConnection())
   {
      con.Open();
      SqlCommand cmd = con.CreateCommand();
      cmd.CommandText = "sp2";
      cmd.CommandType = CommandType.StoredProcedure;
      cmd.Parameters.Add(new SqlParameter("@id", id));

      SqlDataReader reader = cmd.ExecuteReader();

      while (reader.Read())
      {
         ListA listMember = new ListA();
         listMember.ID = (int)reader["ID"];
     listMember.Name = reader["FullName"].ToString().Trim();
      }
      con.Close();
    }
       return listOfRecords;
  }

  public static List<ListA> getRecords(string id)
  {
     List<ListA> listOfRecords = new List<ListA>();
     using (SqlConnection con = SqlConnect.GetDBConnection())
     {
        con.Open();
        SqlCommand cmd = con.CreateCommand();
        cmd.CommandText = "sp1";
        cmd.CommandType = CommandType.StoredProcedure;
        cmd.Parameters.Add(new SqlParameter("@id", id));

        SqlDataReader reader = cmd.ExecuteReader();

        while (reader.Read())
        {
           ListA listMember = new ListA();
           listMember.ID = (int)reader["ID"];
           listMember.Name = reader["FullName"].ToString().Trim();
        }
        con.Close();
      }
      List<ListA> newlist = getRecords(id);
      foreach (ListA x in newlist) listOfRecords.Add(x);
      return listOfRecords;
   }
公共静态列表getRecords2(字符串id)
{
List listOfRecords=新列表();
使用(SqlConnection con=SqlConnect.GetDBConnection())
{
con.Open();
SqlCommand cmd=con.CreateCommand();
cmd.CommandText=“sp2”;
cmd.CommandType=CommandType.storedProcess;
cmd.Parameters.Add(新的SqlParameter(“@id”,id));
SqlDataReader=cmd.ExecuteReader();
while(reader.Read())
{
ListA listMember=新ListA();
listMember.ID=(int)读卡器[“ID”];
listMember.Name=reader[“FullName”].ToString().Trim();
}
con.Close();
}
返回记录列表;
}
公共静态列表getRecords(字符串id)
{
List listOfRecords=新列表();
使用(SqlConnection con=SqlConnect.GetDBConnection())
{
con.Open();
SqlCommand cmd=con.CreateCommand();
cmd.CommandText=“sp1”;
cmd.CommandType=CommandType.storedProcess;
cmd.Parameters.Add(新的SqlParameter(“@id”,id));
SqlDataReader=cmd.ExecuteReader();
while(reader.Read())
{
ListA listMember=新ListA();
listMember.ID=(int)读卡器[“ID”];
listMember.Name=reader[“FullName”].ToString().Trim();
}
con.Close();
}
List newlist=getRecords(id);
foreach(新列表中的listax)listOfRecords.Add(x);
返回记录列表;
}

我在getRecords2中添加了getRecords列表。我做错了吗?

首先,您没有在
while(reader.Read())
循环中向列表添加任何内容<在
GetRecords
GetRecords2
上都缺少code>Add方法调用:

    while (reader.Read())
    {
        ListA listMember = new ListA();

        listMember.ID = (int)reader["ID"];
        listMember.Name = reader["FullName"].ToString().Trim();

        // you have to add this line:
        listOfRecords.Add(listMember);
    }
还有另一个问题:您一遍又一遍地调用
getRecords(id)

List<ListA> newlist = getRecords(id);
foreach (ListA x in newlist) listOfRecords.Add(x);
List newlist=getRecords(id);
foreach(新列表中的listax)listOfRecords.Add(x);
应该是:

List<ListA> newlist = getRecords2(id);
foreach (ListA x in newlist) listOfRecords.Add(x);
List newlist=getRecords2(id);
foreach(新列表中的listax)listOfRecords.Add(x);

最后但并非最不重要的一点是:您不必调用
con.Close()-当程序流退出
时,作为
Dispose
方法的一部分,它将使用
块自动完成。

您在这里有递归调用
List newlist=getRecords(id)看起来getRecords在自我调用?@JakeP和lazyberezovsky。。。非常感谢你们,伙计们…只需要多看几双眼睛来解决我的问题。。。英雄联盟我想我需要休息一下如果我是你,我会打开,看看代码的哪个部分占用的时间最多