名单< T []>。新增()创建副本的C# [英] List<T[]>.Add() creates duplicates c#

查看:179
本文介绍了名单< T []>。新增()创建副本的C#的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我已经围绕System.Data.SQLite的包装方法,它允许我执行参数化查询。

当我尝试添加SQLiteParameter []到列表中。新增(),列表中的所有项目成为了最后一个项目的重复添加。

下面是code:

 字符串SQL =INSERT INTO request_flow(REQUEST_ID,序列,date_assigned+
            VALUES(@request_id,@sequence,@date_assigned)​​;;

名单< SQLiteParameter []> paramList =新的名单,其中,SQLiteParameter []>();

SQLiteParameter []参数=新SQ​​LiteParameter [3];

SQLiteParameter P1 =新SQLiteParameter(REQUEST_ID,DbType.Int32);
SQLiteParameter P2 =新SQLiteParameter(序,DbType.Double);
SQLiteParameter P3 =新SQLiteParameter(date_assigned,DbType.String);

的for(int i = 0; I< assigneeIDs.Count;我++)
{
    p1.Value =请求ID;
    p2.Value = Convert.ToDouble(last_seq +第(i + 1)。);
    p3.Value = DateTime.Now.ToString(ThisAddIn.FullDate);

    参数[0] = P1;
    参数[1] = P2;
    参数[2] = P3;

    paramList.Add(参数);
}

INT插入= SQLite.ExecuteParams(SQL,paramList);
 

我并把一个bearkpoint每个循环进行调试后,我确认是在第一次迭代后的值因为它们的目的。然而,在下一迭代中,第一项(索引0)被以某种方式与第二迭代的值覆盖。因此,添加最后一个项目总是会覆盖列表中的每一个previous项目。

我做了什么错?

编辑:下面是一个简单的(需要更少的依赖)样品code具有相同的行为:

 名单,LT;字符串[]> paramList =新的名单,其中,字符串[]>();

字符串[]参数=新的字符串[3];

串P1 =;
字符串P2 =;
字符串P3 =;

的for(int i = 0;我3;;我++)
{
    P1 = i.ToString()+.1;
    P2 = i.ToString()+.2;
    P3 = i.ToString()+0.3;

    参数[0] = P1;
    参数[1] = P2;
    参数[2] = P3;

    paramList.Add(参数);
}

的foreach(字符串[] S在paramList)
{
    this.textBox1.Text + =的string.join(,S)+\ r \ N的;
}
 

解决方案

您不是在创建每个迭代一个新的参数数组,也不是你在每次迭代创建新的参数,你只是在变异同那些和过度。您需要确保新阵列创建循环的每次迭代,并且为每个循环迭代所创建的新的参数。在这种情况下,你根本不应该界定循环之外这些变量;在循环内将它们定义:

 的for(int i = 0; I< assigneeIDs.Count;我++)
{
    SQLiteParameter []参数=新SQ​​LiteParameter [3];

    SQLiteParameter P1 =新SQLiteParameter(REQUEST_ID,DbType.Int32);
    SQLiteParameter P2 =新SQLiteParameter(序,DbType.Double);
    SQLiteParameter P3 =新SQLiteParameter(date_assigned,DbType.String);

    p1.Value =请求ID;
    p2.Value = Convert.ToDouble(last_seq +第(i + 1)。);
    p3.Value = DateTime.Now.ToString(ThisAddIn.FullDate);

    参数[0] = P1;
    参数[1] = P2;
    参数[2] = P3;

    paramList.Add(参数);
}
 

I have built a wrapper method around System.Data.SQLite that allows me to execute parameterized queries.

When I try to add SQLiteParameter[] to the list by .Add(), all the items in the list become duplicates of the last item added..

Here is the code:

string sql = "INSERT INTO request_flow (request_id, sequence, date_assigned " +
            "VALUES (@request_id, @sequence, @date_assigned);";

List<SQLiteParameter[]> paramList = new List<SQLiteParameter[]>();

SQLiteParameter[] param = new SQLiteParameter[3];

SQLiteParameter p1 = new SQLiteParameter("request_id", DbType.Int32);
SQLiteParameter p2 = new SQLiteParameter("sequence", DbType.Double);
SQLiteParameter p3 = new SQLiteParameter("date_assigned", DbType.String);

for (int i = 0; i < assigneeIDs.Count; i++)
{
    p1.Value = requestID;
    p2.Value = Convert.ToDouble(last_seq + "." + (i + 1));
    p3.Value = DateTime.Now.ToString(ThisAddIn.FullDate);

    param[0] = p1;
    param[1] = p2;
    param[2] = p3;

    paramList.Add(param);
}

int inserted = SQLite.ExecuteParams(sql, paramList);

I did put a bearkpoint after each loop to debug, and I confirmed that the values after the first iteration are as they are intended. However, in the next iteration, the first item (at index 0) gets overwritten somehow with the values of the second iteration. So last item added is always going to overwrite every single previous item in the list.

What did I do wrong?

EDIT: Here is a simpler (less dependency required) sample code that has the same behavior:

List<string[]> paramList = new List<string[]>();

string[] param = new string[3];

string p1 = "";
string p2 = "";
string p3 = "";

for (int i = 0; i < 3; i++)
{
    p1 = i.ToString() + ".1";
    p2 = i.ToString() + ".2";
    p3 = i.ToString() + ".3";

    param[0] = p1;
    param[1] = p2;
    param[2] = p3;

    paramList.Add(param);
}

foreach (string[] s in paramList)
{
    this.textBox1.Text += String.Join(",", s) + "\r\n";
}

解决方案

You're not creating a new parameter array on each iteration, nor are you creating new parameters on each iteration, you're just mutating the same ones over and over. You need to ensure that a new array is created each iteration of the loop, and that new parameters are created for each iteration of the loop. In this case you simply shouldn't define those variables outside of the loop; define them inside of the loop:

for (int i = 0; i < assigneeIDs.Count; i++)
{
    SQLiteParameter[] param = new SQLiteParameter[3];

    SQLiteParameter p1 = new SQLiteParameter("request_id", DbType.Int32);
    SQLiteParameter p2 = new SQLiteParameter("sequence", DbType.Double);
    SQLiteParameter p3 = new SQLiteParameter("date_assigned", DbType.String);

    p1.Value = requestID;
    p2.Value = Convert.ToDouble(last_seq + "." + (i + 1));
    p3.Value = DateTime.Now.ToString(ThisAddIn.FullDate);

    param[0] = p1;
    param[1] = p2;
    param[2] = p3;

    paramList.Add(param);
}

这篇关于名单&LT; T []&GT;。新增()创建副本的C#的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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