为什么增加一个新值列表与LT;>覆盖列表&LT previous值;> [英] Why does adding a new value to list<> overwrite previous values in the list<>

查看:180
本文介绍了为什么增加一个新值列表与LT;>覆盖列表&LT previous值;>的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

以下几个教程和这样我就能够成功地创建继承创建一个数据表,可以被传递到SQL Server的存储过程作为一个表值参数所需要的功能的集合类。一切似乎运作良好;我可以得到所有添加的行,它看上​​去很美。然而,仔细观察我发现,当我添加一个新行,所有的previous行中的数据被覆盖新行的值。所以,如果我有一排foo的字符串值,我添加第二个行具有值栏中的,第二行会被插入(使两行的数据表),但两行的值将为吧。任何人都可以明白为什么这会是什么?下面是一些code,其工作原理,但已经有点简化(标签类已经减少为便于解释)。

以下是集合类的:

 使用系统;
使用System.Collections.Generic;
使用System.Data这;
使用System.Linq的;
使用的System.Web;
使用Microsoft.SqlServer.Server;

命名空间TagTableBuilder
{
公共类TagCollection:列表<标签>中的IEnumerable<的SqlDataRecord>
{
    IEnumerator的<的SqlDataRecord> IEnumerable的<的SqlDataRecord> .GetEnumerator()
    {
        VAR特别提款权=新的SqlDataRecord(
            新SqlMetaData(标签,SqlDbType.NVarChar)
            );

        的foreach(在此标签T)
        {
            sdr.SetSqlString(0,t.tagName);

            得到的回报特别提款权;
        }
    }
}

公共类标签
{
    公共字符串标记名{获得;组; }
}
}
 

这些被称为如下:

  //创建集合实例
TagCollection标签=新TagCollection();

//创建对象实例
标签_tag =新标签();

的foreach(在标记列表串T)
{
    //添加值上课欢迎使用属性
    _tag.tagName = T;
    //添加类的收集,这是所有previously添加的行覆盖
    tags.Add(_tag);
}
 

解决方案

您正在使用标签的同一个实例对象的循环中,所以每次更新至标签名是相同的。将申报循环中得到一个新的对象每次通过循环的:

 的foreach(在标记列表串T)
{
    标签_tag =新标签();

    //添加值上课欢迎使用属性
    _tag.tagName = T;
    //添加类的收集,这是所有previously添加的行覆盖
    tags.Add(_tag);
}
 

还要注意,我更新了最后一行添加 _tag 而不是 MTAG ,因为我没有看到这个任何地方定义的。

Following a few tutorials and such I was able to successfully create a collection class which inherits the functionality needed to create a DataTable which can be passed to a Sql Server's stored procedure as a table value parameter. Everything seems to be working well; I can get all of the rows added and it looks beautiful. However, upon closer inspection I notice that when I add a new row, the data for all of the previous rows is overwritten with the value for the new row. So if I have a row with a string value of "foo" and I add a second row with the value "bar", the second row will be inserted (making a DataTable with two rows) but both rows will have the value "bar". Can anyone see why this would be? Here is some of the code, which works but has been a bit simplified (the Tag class has been reduced for ease of explanation).

The following is the Collection class's:

using System;
using System.Collections.Generic;
using System.Data;
using System.Linq;
using System.Web;
using Microsoft.SqlServer.Server;

namespace TagTableBuilder
{
public class TagCollection : List<Tag>, IEnumerable<SqlDataRecord>
{
    IEnumerator<SqlDataRecord> IEnumerable<SqlDataRecord>.GetEnumerator()
    {
        var sdr = new SqlDataRecord(
            new SqlMetaData("Tag", SqlDbType.NVarChar)
            );

        foreach (Tag t in this)
        {
            sdr.SetSqlString(0, t.tagName);

            yield return sdr;
        }
    }
}

public class Tag
{
    public string tagName { get; set; }
}
}

These are called as follows:

//Create instance of collection
TagCollection tags = new TagCollection();

//Create instance of object
Tag _tag = new Tag();

foreach (string t in tagList)
{
    //Add value to class propety 
    _tag.tagName = t;
    //Add class to collection, this is where all previously added rows are overwritten
    tags.Add(_tag);
}

解决方案

You're using the same instance of the Tag object inside the loop, so each update to the tagName is to the same reference. Move the declaration inside the loop to get a fresh object on each pass of the loop:

foreach (string t in tagList)
{
    Tag _tag = new Tag();

    //Add value to class propety 
    _tag.tagName = t;
    //Add class to collection, this is where all previously added rows are overwritten
    tags.Add(_tag);
}

Also notice that I updated the last line to add _tag instead of mTag as I don't see this defined anywhere.

这篇关于为什么增加一个新值列表与LT;&GT;覆盖列表&LT previous值;&GT;的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
相关文章
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆