当我更新具有相同数据类型的另一个列表时,列表被更改
本文关键字:列表 另一个 数据类型 更新 | 更新日期: 2023-09-27 18:17:36
我的代码是这样的
public class Category
{
public string CatId { get; set; }
public string CatName { get; set; }
public string SpecId { get; set; }
public string SpecName { get; set; }
}
List<Category> LstCategory = new List<Category>();
List<Category> LstCatWithSpec = new List<Category>();
现在,我想首先在LstCatWithSpec
中添加category。我在LstCategory
中复制这个列表,然后在LstCatWithSpec
中添加专业。现在,当我用专业更新列表时,我只复制类别的列表也发生了变化。它也充满了特色。它不应该改变,对吗?
当我有两个不同的对象时,当我给它添加项时,我给了特定的对象名称,这怎么可能呢?
由于我在xamarin.forms
上工作,我已经标记了xamarin.forms
,因为我不知道它是否存在于c#
中。
EDIT -
我尝试使用foreach
循环添加列表。
foreach( var item in LstCatWithSpec)
{
LstCategory.Add(item);
}
我也试过这个。
LstCategory = new List<Category>(LstCatWithSpec);
我尝试了这种方法,并成功了
List<DB> List1= new List<DB>();
List<DB> List2= new List<DB>();
var DBData = List1.FirstOrDefault();
List2.Add(new DB()
{
field1= DBData.field1,
field2= DBData.field2,
field3= DBData.field3,
});
List1.AddRange(List2);