今天写代码的时候遇到如下问题:
1.当TabMaterialInquirylList.Insert(0,TabMater);操作的时候是对的,没有出现重复数据
2.可是当
TabMaterialInquirylList.Insert(0,TabMater);
AllTabMaterialInquirylList.Insert(0, TabMater);
操作的时候会出现重复数据。
3.可是我必须向两个集合插入数据,于是这样解决了,插入数据之后Distinct一下就好了。
TabMaterialInquirylList.Insert(0,TabMater);
AllTabMaterialInquirylList.Insert(0, TabMater);
TabMaterialInquirylList =new ObservableCollection<TabMaterialInquiry>(TabMaterialInquirylList.Distinct().ToList());
AllTabMaterialInquirylList= new ObservableCollection<TabMaterialInquiry>(AllTabMaterialInquirylList.Distinct().ToList());
我也实验了TabMaterialInquirylList.Add(TabMater);也出现同样的问题,百思不得其解,所以想了解为什么向多个集合里面同时插入子元素会出现这个问题(重复数据的问题)?
难道你的多个集合是引用内存的同一处
没 看过了
你要 贴整个代码
TabMaterialInquirylList和AllTabMaterialInquirylList 是什么样子的,在哪里声明的
TabMater 是什么样子的
TabMaterialInquirylList和AllTabMaterialInquirylList 是什么样子的
public ObservableCollection<TabMaterialInquiry> _tabMaterialInquirylList;
public ObservableCollection<TabMaterialInquiry> TabMaterialInquirylList
{
get { return _tabMaterialInquirylList; }
set
{
if (!ReferenceEquals(_tabMaterialInquirylList, value))
{
_tabMaterialInquirylList = value;
this.RaisePropertyChanged(() => this.TabMaterialInquirylList);
}
}
}
public ObservableCollection<TabMaterialInquiry> _alltabMaterialInquirylList;
public ObservableCollection<TabMaterialInquiry> AllTabMaterialInquirylList
{
get { return _alltabMaterialInquirylList; }
set
{
if (!ReferenceEquals(_alltabMaterialInquirylList, value))
{
_alltabMaterialInquirylList = value;
this.RaisePropertyChanged(() => this.AllTabMaterialInquirylList);
}
}
}
TabMater 是什么样子的:
private void OnAddCommand(object param)
{
TabMaterialInquiry TabMater = new TabMaterialInquiry();
TabMater.CreateTime = DateTime.Now;
TabMater.CreateUserId = ApplicationContext.Current.User.Id;
TabMater.CreateUserName = ApplicationContext.Current.User.Name;
TabMater.EditState = EditState.Insert;
TabMater.Id = Guid.NewGuid().ToString();
if (AllTabMaterialInquirylList == null)
{
AllTabMaterialInquirylList = new ObservableCollection<TabMaterialInquiry>();
}
if (TabMaterialInquirylList == null)
{
TabMaterialInquirylList = new ObservableCollection<TabMaterialInquiry>();
}
TabMaterialInquirylList.Insert(0, TabMater);
AllTabMaterialInquirylList.Insert(0, TabMater);
TabMaterialInquirylList = new ObservableCollection<TabMaterialInquiry>(TabMaterialInquirylList.Distinct().ToList());
AllTabMaterialInquirylList = new ObservableCollection<TabMaterialInquiry>(AllTabMaterialInquirylList.Distinct().ToList());
}
@木耳:
好复杂,呵呵 没看出什么问题, 你的 TabMaterialInquirylList属性中的
this.RaisePropertyChanged(() => this.TabMaterialInquirylList); 是什么样子的