从 ObservableCollection 类中删除记录

发布于 2024-10-20 23:03:01 字数 597 浏览 2 评论 0原文

我正在开发 WPF 应用程序,并且收到错误,这意味着我无法从 ObservableCollectionClass 中删除项目。我的代码如下所示。

它正在工作,但记录未删除。

        SampleDB conn = new SampleDB(Helper.GetPath());

        var Query = from a in conn.UserInfo
                    where a.ID == (int)iSelectedID
                    select new UserDatail { ID = a.ID, Name = a.Name, Address = a.Address, City = a.City, Pin = a.Pin, Phone = a.Phone };

        foreach (var item in Query)
        {
            userDetail.Remove(item);

        }
        dgPorfomance.ItemsSource = userDetail;
        dgPorfomance.Items.Refresh();

I am Working on WPF Application and I am getting error means I am not able to Remove Item from the ObservableCollectionClass My Code is given bellow..

it's working but the record is not delete.

        SampleDB conn = new SampleDB(Helper.GetPath());

        var Query = from a in conn.UserInfo
                    where a.ID == (int)iSelectedID
                    select new UserDatail { ID = a.ID, Name = a.Name, Address = a.Address, City = a.City, Pin = a.Pin, Phone = a.Phone };

        foreach (var item in Query)
        {
            userDetail.Remove(item);

        }
        dgPorfomance.ItemsSource = userDetail;
        dgPorfomance.Items.Refresh();

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(3

和我恋爱吧 2024-10-27 23:03:01

ObservableCollection 找不到您要删除的对象,因为它与集合中当前的对象不同(您刚刚使用 new 创建了该对象)。

您需要重写 UserDetail 类中的 Equals ,以便可以根据您的规则测试两个实例的相等性:

public override bool Equals(object o)
{
    UserDetail other = o as UserDetail;
    if (other != null)
    {
        return this.Id == other.Id;
    }
    return false;
}

请注意,当您重写 Equals 时,您还必须重写 GetHashCode

public override int GetHashCode()
{
    return this.Id.GetHashCode();
}

The ObservableCollection can't find the object you want to remove, because it's different from the one currently in the collection (you just created it with new).

You need to override Equals in your UserDetail class so that two instances can be tested for equality based on your rules:

public override bool Equals(object o)
{
    UserDetail other = o as UserDetail;
    if (other != null)
    {
        return this.Id == other.Id;
    }
    return false;
}

Note that when you override Equals, you must also override GetHashCode:

public override int GetHashCode()
{
    return this.Id.GetHashCode();
}
东风软 2024-10-27 23:03:01

您正试图从最初不包含该对象的集合中删除一个对象。

解释:

当您这样做时,

foreach (var item in Query)
    {
        userDetail.Remove(item);
    }

您必须确保循环中的每个项目与集合中包含的项目是同一实例。在这里,情况并非如此:这些项目可能看起来相同,但它们实际上是同一项目的 2 个不同实例,因为您首先从查询而不是从集合中获取这些项目,

所以您应该执行类似的操作在这种情况下:

foreach (var item in Query)
    {
        foreach (var itemInCollection in userDetail)
        {
             if (itemInCollection.Id == item.Id)
                 userDetail.Remove(itemInCollection );
        }
    }

you are trying to remove an object from a collection that does not contain this object in the first place.

explanation:

when you do

foreach (var item in Query)
    {
        userDetail.Remove(item);
    }

you have to be sure that each item in your loop is the same instance than the item contained in the collection. Here, this is not the case: the items may appear to be the same, but they are actually 2 different instances of the same item, as you first get those items from a query and not from your collection

so you should do something like this in this case:

foreach (var item in Query)
    {
        foreach (var itemInCollection in userDetail)
        {
             if (itemInCollection.Id == item.Id)
                 userDetail.Remove(itemInCollection );
        }
    }
怪我鬧 2024-10-27 23:03:01

我认为错误可能是“集合已修改;枚举操作可能无法执行”。

这就是使用 ienumerable 接口迭代集合的 foreach 语句的问题。为了克服这个问题,您可以制作当前集合 [.ToList() 方法] 的浅表副本并迭代每个集合。

解决办法是:

foreach(Query.ToList()中的var item)
{
userDetail.Remove(item);
}

I think the error might be 'Collection was modified; enumeration operation may not execute'.

This is the problem of foreach statement with ienumerable interface to iterate through a collection. To overcome this you can make a shallow copy of the present collection[.ToList() method] and iterate through each.

The solution is:

foreach (var item in Query.ToList())
{
userDetail.Remove(item);
}

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文