如何为列表插入初始值,其中已经添加了数据,mvc,c#

How to insert an initial values for the list, which have already added data in it, mvc, c#

我已将数据添加到这样的列表中。

List<CommonSearch> regionList = new List<CommonSearch>();

            foreach(var items in filteredRegion)
            {
                regionList.Add(new CommonSearch 
                {
                    Destination = returnRegionName(items),
                    regCde = items
                });

            }

现在我想在下面添加 this.like 的初始数据以显示在我的 dropdownlist.how 中,我可以这样做吗?

//regionList.Insert(0,"All");

foreach 循环后插入 regionList 新的 CommonSearch 项目:

regionList.Insert(0, new CommonSearch() { Destination = "All" });

由于列表中的类型不匹配,您的插入代码将不起作用。

List<> 确实保证顺序,您可以在开头添加元素。

List<CommonSearch> regionList = new List<CommonSearch>();
regionList.Add(new CommonSearch { Destination = "All" });

或在循环后的第 0 位置插入。

regionList.Insert(0, new CommonSearch { Destination = "All" });