試圖檢查我的ObservableCollection沒有匹配我想要添加的對象。LINQ Check ObservableCollection不包含匹配對象
例如:
public class LoggedUsers : NotifyUIBase
{
public ObservableCollection<Logged> UserList { get; private set; }
public LoggedUsers()
{
UserList = new ObservableCollection<Logged>();
}
public void Add(Logged user)
{
if (UserList.Where(x => x == user))
{
UserList.Add(user);
}
}
}
因此,如果的ObservableCollection得到了一個登錄對象具有相同的屬性值作爲Add(Logged user)
話,我希望它不添加user
。
編輯1:
public class LoggedUsers : NotifyUIBase
{
public ObservableCollection<Logged> UserList { get; private set; }
public LoggedUsers()
{
UserList = new ObservableCollection<Logged>();
}
public void Add(Logged user)
{
if (!UserList.Any(x => x == user))
{
UserList.Add(user);
////Check the List for users
UpdateView();
}
else
{
MessageBox.Show("Already exists!");
}
}
編輯2: 改變了我的登錄級以下幾點:
namespace PhotoManagement
{
public class Logged : Common.NotifyUIBase
{
public string ClearPassword { get; set; }
public string FirstName { get; set; }
public string LastName { get; set; }
public long UID { get; set; }
public string Email { get; set; }
//Display basic statistics
public int ThisDayImageCount { get; set; }
public int ThisDaySaleCount { get; set; }
public Logged()
{
//Update the stats when instigated
UpdateStats();
}
//Update the stats
public void UpdateStats()
{
}
public bool Equals(Logged other)
{
if (other == null) return false;
return (this.UID.Equals(other.UID));
}
}
}
所以這應該工作?但是,當我檢查實例時它仍然存在,並且它們是不同的。
public class LoggedUsers : NotifyUIBase
{
public ObservableCollection<Logged> UserList { get; private set; }
public LoggedUsers()
{
UserList = new ObservableCollection<Logged>();
}
public void Add(Logged user)
{
if (!UserList.Any(x => x.Equals(user)))
{
UserList.Add(user);
UpdateView();
}
else
{
MessageBox.Show("Already exists!");
}
}
你的問題到底是什麼? –