C# 如何将项目添加到 List<T> 的开头?

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/390491/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-04 01:40:29  来源:igfitidea点击:

How to add item to the beginning of List<T>?

c#drop-down-menugeneric-list

提问by Ash Machine

I want to add a "Select One" option to a drop down list bound to a List<T>.

我想将“选择一个”选项添加到绑定到List<T>.

Once I query for the List<T>, how do I add my initial Item, not part of the data source, as the FIRST element in that List<T>? I have:

一旦我查询了List<T>,我该如何添加我的初始Item元素,而不是数据源的一部分,作为其中的第一个元素List<T>?我有:

// populate ti from data               
List<MyTypeItem> ti = MyTypeItem.GetTypeItems();    
//create initial entry    
MyTypeItem initialItem = new MyTypeItem();    
initialItem.TypeItem = "Select One";    
initialItem.TypeItemID = 0;
ti.Add(initialItem)  <!-- want this at the TOP!    
// then     
DropDownList1.DataSource = ti;

采纳答案by Matt Hamilton

Use the Insertmethod:

使用插入方法:

ti.Insert(0, initialItem);

回答by x0n

Update: a better idea, set the "AppendDataBoundItems" property to true, then declare the "Choose item" declaratively. The databinding operation will add to the statically declared item.

更新:一个更好的主意,将“AppendDataBoundItems”属性设置为true,然后以声明方式声明“Choose item”。数据绑定操作将添加到静态声明的项目。

<asp:DropDownList ID="ddl" runat="server" AppendDataBoundItems="true">
    <asp:ListItem Value="0" Text="Please choose..."></asp:ListItem>
</asp:DropDownList>

http://msdn.microsoft.com/en-us/library/system.web.ui.webcontrols.listcontrol.appenddatabounditems.aspx

http://msdn.microsoft.com/en-us/library/system.web.ui.webcontrols.listcontrol.appenddatabounditems.aspx

-Oisin

-Oisin

回答by Sina Lotfi

Use Insertmethod of List<T>:

使用插入方法List<T>

List.Insert Method (Int32,?T): Insertsan element into the List at the specified index.

List.Insert Method(Int32,?T):将Inserts一个元素插入到List处specified index

var names = new List<string> { "John", "Anna", "Monica" };
names.Insert(0, "Micheal"); // Insert to the first element

回答by sonny

Use List<T>.Insert

List<T>.Insert

While not relevant to your specific example, if performance is important also consider using LinkedList<T>because inserting an item to the start of a List<T>requires all items to be moved over. See When should I use a List vs a LinkedList.

虽然与您的具体示例无关,但如果性能很重要,也可以考虑使用,LinkedList<T>因为将项目插入到 a 的开头List<T>需要将所有项目移过。请参阅何时应该使用 List 与 LinkedList

回答by aloisdg moving to codidact.com

Since .NET 4.7.1, you can use the side-effect free Prepend()and Append(). The output is going to be an IEnumerable.

从 .NET 4.7.1 开始,您可以免费使用副作用Prepend()Append(). 输出将是一个 IEnumerable。

// Creating an array of numbers
var ti = new List<int> { 1, 2, 3 };

// Prepend and Append any value of the same type
var results = ti.Prepend(0).Append(4);

// output is 0, 1, 2, 3, 4
Console.WriteLine(string.Join(", ", results ));