C# while(reader.Read()){}中列表中的重复值

C# while(reader.Read()){}中列表中的重复值,c#,asp.net,list,reader,C#,Asp.net,List,Reader,我已经单独测试了这个程序,效果很好。然而,当我测试上面创建的Web服务时,我得到了一个列表,其中最后一个值沿着整个列表复制 有人能帮我弄清楚为什么要一次又一次地重复相同的(最后一个)值吗?通过在循环之前创建报表对象并重复使用,可以在列表中多次插入对该对象的引用 您应该在循环中创建报表对象: CREATE Proc [dbo].[getAllReportsByUserID] @user_id int as Begin Select id, title, descr

我已经单独测试了这个程序,效果很好。然而,当我测试上面创建的Web服务时,我得到了一个列表,其中最后一个值沿着整个列表复制


有人能帮我弄清楚为什么要一次又一次地重复相同的(最后一个)值吗?

通过在循环之前创建报表对象并重复使用,可以在列表中多次插入对该对象的引用

您应该在循环中创建报表对象:

CREATE Proc [dbo].[getAllReportsByUserID]
@user_id int
as
Begin
  Select 
    id, 
    title, 
    description, 
    anonymous, 
    location, 
    status, 
    category, 
    date, 
    picture_url, 
    admin_id 
  from reports 
  where user_id = @user_id
End

通过在循环之前创建报表对象并重复使用它,可以在列表中多次插入对同一对象的引用

您应该在循环中创建报表对象:

CREATE Proc [dbo].[getAllReportsByUserID]
@user_id int
as
Begin
  Select 
    id, 
    title, 
    description, 
    anonymous, 
    location, 
    status, 
    category, 
    date, 
    picture_url, 
    admin_id 
  from reports 
  where user_id = @user_id
End

如果您为读卡器的每次读取创建一个新报告,它会改变行为吗?如果您为读卡器的每次读取创建一个新报告,它会改变行为吗?耶!谢谢你@ConnorsFan!:我会尽快接受你的回答:再见!谢谢你@ConnorsFan!:我会尽快接受你的答复
        SqlDataReader reader = cmd.ExecuteReader();
        while (reader.Read())
        {
            reports report = new reports();
            report.id = Convert.ToInt32(reader["id"]);
            report.title = reader["title"].ToString();
            report.description = reader["description"].ToString();
            report.anonymous = (bool)reader["anonymous"];
            report.location = reader["location"].ToString();
            report.status = reader["status"].ToString();
            report.category = reader["category"].ToString();
            report.date = (DateTime)reader["date"];
            report.picture_url = reader["picture_url"].ToString();
            report.admin_id = Convert.ToInt32(reader["admin_id"]);
            repers.Add(report);
        }
        return repers;