C# 是否可以在一行代码中动态添加列表值?

C# 是否可以在一行代码中动态添加列表值?,c#,asp.net,string,list,C#,Asp.net,String,List,我有一个项目,我从formview字段中获取字符串40+字段的值。我希望减少代码中的行数,并希望为从formview收集的每个字符串值将添加列表值合并到一行代码中 我的代码片段是: ... List<string> mylist = new List<string>(); string str1 = e.NewValues["value1"].ToString(); myList.Add(str1); string str2 = e.NewValues["value2"

我有一个项目,我从formview字段中获取字符串40+字段的值。我希望减少代码中的行数,并希望为从formview收集的每个字符串值将添加列表值合并到一行代码中

我的代码片段是:

...
List<string> mylist = new List<string>();

string str1 = e.NewValues["value1"].ToString();
myList.Add(str1);
string str2 = e.NewValues["value2"].ToString();
myList.Add(str2);
string str3 = e.NewValues["value3"].ToString();
myList.Add(str3);
string str4 = e.NewValues["value4"].ToString();
myList.Add(str4);
...

我对sql参数做了类似的操作 即

-但我已经寻找了很多方法试图做到这一点,但没有任何结果。我不确定我是否只是没有正确地搜索它,或者这是不可能的?

试试这个:

myList.Add(e.NewValues["value1"].ToString());
您可以像这样初始化它:

Test tester = new Test{ test1 = "some string", test2 = 5 };
你可以用

myList.AddRange(
   new[]{  
        e.NewValues["value1"].ToString(),   
        e.NewValues["value2"].ToString()  
});
或者使用内联赋值:

List<string> mylist = new List<string>
{
   e.NewValues["value1"].ToString(),
   e.NewValues["value2"].ToString()
};
使用Linq怎么样

比如:

myList.Add(string str1 = e.NewValues["value1"].ToString());
List<string> mylist = new List<string>();

myList.AddRange(e.NewValues.ToList().Select(s => s.ToString());

NewValues是一个字典,因此类似这样的字典将在一行中工作:

 List<string> mylist = e.NewValues.Values.ToList().ConvertAll(x => x.ToString());
更新:

NewValues返回的是orderedDictionary,因此在eventHandler中放置以下代码:

List<string> mylist = new List<string>();

foreach (DictionaryEntry item in e.NewValues)
{
    mylist.Add(item.Value.ToString());
}

或者,如果mylist是类成员变量,则只有foreach循环将填充列表。

mylist.Adde.NewValues[value1].ToString;啊!我想的太多了,太复杂了。你所有的回答都有效。感谢您的快速回复。什么是新价值观?列表?myList=e.NewValues.Selectx=>x.ToString.ToList;NewValues是我的FormView中的一个事件参数。我只需要建立一个值列表来传递给更新存储过程。ToList不是值之后的选项,而是if…values.ToString.ToList。。。已添加,但它不会返回我要查找的字段值。FormView还有标签和下拉列表,我从中获取值并更新数据库中的记录。如果我能在一行中从formview中获取所有控件值,那就太棒了!我正在使用“对象发送者,FormViewUpdateEventArgs e”-如果有更好的方法,我是开放的。@ACJohnson 72刚刚根据您的评论编辑,希望能有所帮助。
<form id="form1" runat="server">
<div>
<table>
<tr>    <td>    <asp:TextBox ID="txxt" runat="server" />    </td>    </tr>
<tr>    <td>    <asp:Button ID="btn" Text="test" runat="server" style="width: 36px" />    </td>    </tr>
<tr>    <td>  <asp:HiddenField ID="hdntest" runat="server" />    </td>    </tr>
</table>
</div>
</form>
List<string> mylist = new List<string>();

myList.AddRange(e.NewValues.ToList().Select(s => s.ToString());
e.NewValues.ToList().ForEach(v => 
                        {
                          //can add additional manipulation or If clause here
                          //if you have some exclusions
                          myList.Add(v.ToString())
                         });
 List<string> mylist = e.NewValues.Values.ToList().ConvertAll(x => x.ToString());
List<string> mylist = new List<string>();

foreach (DictionaryEntry item in e.NewValues)
{
    mylist.Add(item.Value.ToString());
}
<form id="form1" runat="server">
<div>
<table>
<tr>    <td>    <asp:TextBox ID="txxt" runat="server" />    </td>    </tr>
<tr>    <td>    <asp:Button ID="btn" Text="test" runat="server" style="width: 36px" />    </td>    </tr>
<tr>    <td>  <asp:HiddenField ID="hdntest" runat="server" />    </td>    </tr>
</table>
</div>
</form>
Protected Sub btn_Click(ByVal sender As Object, ByVal e As EventArgs) Handles btn.Click

    Dim s As String = ""
    s = txxt.Text
    s = s + ","
    hdntest.Value = hdntest.Value + s
    txxt.Text = hdntest.Value

End Sub