publicclassMyDataModel{publicint Id {get;set;}publicstring Name {get;set;}}publicstaticvoidMain(){// 创建 ObservableCollectionObservableCollection<MyDataModel> myDataCollection =newObservableCollection<MyDataModel>{newMyDataModel{ Id =1, Name ="Item 1"},newMyDataModel{ Id =2, Name ="Item 2"},newMyDataModel{ Id =3, Name ="Item 3"}};// 要添加的项MyDataModel newItem =newMyDataModel{ Id =2, Name ="Item 2"};// Id 已存在// 使用 LINQ 检查是否已存在if(!myDataCollection.Any(item => item.Id == newItem.Id)){myDataCollection.Add(newItem);Console.WriteLine($"Item with Id {newItem.Id} added.");}else{Console.WriteLine($"Item with Id {newItem.Id} already exists.");}// 输出当前集合中的项foreach(var item in myDataCollection){Console.WriteLine($"Id: {item.Id}, Name: {item.Name}");}}
外贸SOHO(Small Office Home Office)企业正以前所未有的速度崛起,然而,要在这片竞争激烈的蓝海中立足,高效的全球通信能力、坚实的安全防线、成本效益的考量以及专业的品牌形象塑造,缺一不可。本文旨在为外…